【问题标题】:Error with web.py uploading <type 'exceptions.IOError'>web.py 上传时出错 <type 'exceptions.IOError'>
【发布时间】:2012-12-16 14:44:14
【问题描述】:

所以我正在按照 web.py 上传和存储指南进行测试,但我一直得到一个 错误指出 [Errno 2] 没有这样的文件或目录:

这是我的代码

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() 

帮助谢谢

【问题讨论】:

    标签: python web.py uploading storage


    【解决方案1】:
    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/

    【讨论】:

      【解决方案2】:

      我自己也遇到了同样的问题,并试图在这里问这个问题。但是没有人回答。

      终于搞清楚是什么问题了,想和大家分享一下!

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2013-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多