web.py上传错误<type'exception.ioerror'=“”> </type>

时间:2013-01-02 07:49:31

标签: python web.py uploading storage

所以我按照web.py上传和存储指南来测试它,但我一直在测试  错误说明[Errno 2]没有这样的文件或目录:&lt;帮助

这是我的代码

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

我自己遇到了同样的问题,并试着在这里问一下。但没有人回答。

最后,我想出了问题的所在,并希望与您分享!

这部分:filedir = '/project/webtest/templates'应该是绝对路径。

它应该是一个现有的目录(至少在我的踪迹中它应该是一个现有的目录,否则它会提示你发布的相同错误)!该文件不必令人兴奋,因为我们将通过复制上传的文件来创建它。

例如在我的Mac中,它是'/Users/J/pythonex/projects/gothonweb/docs',它是现有目录。如果它不是现有目录,您将收到相同的错误消息。

最后,最棘手的部分。我的mac,上传的文件实际存储在我的磁盘中的确切目录中。但是在我重新发现取景器之前,我无法在我的发现者中看到它们。我不知道为什么会这样。但是对于我的电脑来说,就是这样。