2013-01-02 29 views
0

所以我跟隨web.py上傳和存儲指南來測試它,但我一直geting 錯誤,指出[錯誤2]沒有這樣的文件或目錄:<幫助錯誤與web.py上傳<type'exceptions.IOError'>

這是我的代碼

import web 

urls = (
    '/hello', 'index', 
    '/hello/upload', 'upload' 
) 
app = web.application(urls, globals()) # handels http request that aks for urls 
# the base ells lpthw.web to use the templates/layout.html file as the base template for all the other templates 
render = web.template.render('templates/', base="layout") 
# must use port 127.0.0.1:5000 
class index(object): 

    def GET(self): 
     return render.hello_form() 

    def POST(self): 
     form = web.input(name="Nobody", greet="Hello") 
     greeting = "%s, %s" % (form.greet, form.name) 
     return render.index(greeting = greeting) 

class upload(object): 

    def POST(self): 
     x = web.input(files={}) 

     filedir = '/project/webtest/templates' # directory were you want to save the file 
     if 'files' in x: # check if the file -object is created 
      filepath=x.files.filename.replace('\\','/') # replace the windows -style slashes with linux ones 
      filename=filepath.split('/')[-1] #splits the and chooses the last part (the filename with extension 
      fout = open(filedir +'/'+ filename,'w') # creates the file where the uploaded file should be stored 
      fout.write(x.files.file.read()) #writes the uploaded file to the newly created file.    
      fout.close() #closes the file 
     else: 
      return "Error no file" 


if __name__ == "__main__": 
    app = web.application(urls, globals()) 
    app.run() 

幫助THX

回答

0
import web 

urls = ('/upload', 'Upload') 

class Upload: 
def GET(self): 
    web.header("Content-Type","text/html; charset=utf-8") 
    return view.upload() 
def POST(self): 
    x = web.input(myfile={}) 
    filedir = '/path/where/you/want/to/save' # change this to the directory you want to store the file in. 
    if 'myfile' in x: # to check if the file-object is created 
     filepath=x.myfile.filename.replace('\\','/') 
     filename=filepath.split('/')[-1] 
     fout = open(filedir +'/'+ filename,'w') 
     fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file. 
     fout.close() # closes the file, upload complete. 
    raise web.seeother('/upload') 


if __name__ == "__main__": 
    app = web.application(urls, globals()) 
    app.run() 


upload.html 
<html> 
<head> 
<title>File upload</title> 
</head> 
<body> 
<form method="POST" enctype="multipart/form-data" action=""> 
    <input type="file" name="myfile" /><br/> 
    <input type="submit" /> 
</form> 
</body> 
</html> 

供您參考http://webpy.org/cookbook/storeupload/

0

我自己遇到了同樣的問題,並試圖在這裏提出這個問題。但沒有人回答。

最後,我想出了什麼問題,並想與大家分享!

這部分:filedir = '/project/webtest/templates'應該是絕對路徑。

它應該是一個現有的目錄(至少在我的路徑中它應該是一個現有的目錄,否則它會提示你發佈的同樣的錯誤)!由於我們要通過複製上傳的文件來創建文件,因此該文件不需要激動人心。

例如在我的mac中,它是'/Users/J/pythonex/projects/gothonweb/docs',它是一個現有的目錄。如果它不是一個現有的目錄,你會得到相同的錯誤信息。

最後,最棘手的部分。我的我的Mac,上傳的文件實際上存儲在我的磁盤在該確切的目錄。但在我重新啓動取景器之前,我無法在發現者身上看到它們。我不知道那是爲什麼。但對於我的電腦來說,情況就是這樣。