2013-11-24 63 views
0

我使用jQuery表單插件上傳文件。這些文件被髮送到我的cherrypy腳本並返回到我的jQuery,然後將文件名追加到我的頁面。系統在localhost上運行良好。我使用webfaction作爲虛擬主機,當我嘗試使用表單插件上傳文件時,我在我的jQuery錯誤日誌中出現以下錯誤:jQuery表單插件在錯誤位置搜索頁面

2013/11/24 16:41:26 [error] 26628#0: * 2912993 open()「/ home/mywebsite/webapps/htdocs/submit」failed(2:No such file or directory),client:5.100.131.14,server:mywebsite.webfactional.com,request:「POST/submit HTTP/1.1「,主機:」mywebsite.webfactional.com「,referrer:」http://mywebsite.webfactional.com/freelinreg

奇怪的是,它試圖打開提交文件在」/ home/mywebsite/webapps/htdocs/submit「 exisit。通常,根據我的代碼,cherrypy似乎在做的是在運行我的電腦時使用「http://mywebsite.webfactional.com/freelinreg/submit」或「localhost:8080/submit」提供'/ submit'。

有沒有辦法指出jQuery Form Plugin在「http://mywebsite.webfactional.com/freelinreg/submit」而不是「/ home/mywebsite/webapps/htdocs/submit」處尋找'/ submit'?

類根(對象):

@cherrypy.expose 
def index(self) 
    return open('/home/joestox/webapps/freelinreg_static/index.html') 

@cherrypy.expose 
def submit(self, myfile): 

    cherrypy.session['myfile'] = myfile 
    data_name = myfile.filename 

    #Send back to JQuery with Ajax 
    #Put in JSON form 
    data_name= json.dumps(dict(title = data_name)) 
    cherrypy.response.headers['Content-Type'] = 'application/json' 

    return data_name 

HTML:

<!DOCTYPE html> 
    <html> 
     <head> 
      <script type='text/javascript' src='freelinreg_static/google.js'></script> 
      <script type='text/javascript' src='freelinreg_static/frontend.js'></script> 
      <script type='text/javascript' src='freelinreg_static/malsup.js'></script> 
     </head> 
     <body> 

     <form id="dataform" action="submit" method="post" enctype="multipart/form-data"> 
      <input type="file" name="myfile" id="myFile"/> 
      <input type="submit" id="data_submit" value="Continue"/> 
     </form>       

     </body> 
    </html> 

的jQuery(frontend.js):

$(document).ready(function() { 
    (function() { 
     $('#dataform').ajaxForm({ 
      success: function (data) { 
       var $a_var = data['title']; 
       $('body').append($a_var); 
      } 
     }); 
     return false; 
    })(); 
}); 
+0

看來你在混合本地目錄和網絡路徑。你有TMP/TEMP環境變量設置是否正確?你可以提供Python錯誤轉儲(你可能想在控制檯中運行它)? – jwalker

回答

0

jwalker是正確的。你混淆了網址路徑和物理路徑。您的cherrypy應用程序需要系統上的物理位置來存儲文件。我也沒有看到你試圖將上傳的文件保存在任何地方。

@cherrypy.expose 
def submit(self, myfile): 

    cherrypy.session['myfile'] = myfile 
    data_name = myfile.filename 
    upload_path = '/home/mywebsite/webapps/submit' + myfile.filename 

    size = 0 
    all_data = bytearray() 
    while True: 
     data = myfile.file.read(8192) 
     all_data += data 
     if not data: 
      break 
     size += len(data) 

    saved_file=open(upload_path, 'wb') 
    saved_file.write(all_data) 
    saved_file.close() 

    #Send back to JQuery with Ajax 
    #Put in JSON form 
    data_name= json.dumps(dict(title = data_name)) 

    return data_name 

此外,它看起來像你想然後能夠讓用戶查看該文件。如果是這種情況,您還需要打開cherrypy的靜態瀏覽(如果您沒有提供靜態內容的web服務器)。

cherrypy.config.update({'tools.staticdir.on': True, 
       'tools.staticdir.dir': '/home/mywebsite/webapps/submit' 
       }) 

希望這會有所幫助!