0

所以我正在按照 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() 

帮助谢谢

4

2 回答 2

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/

于 2013-03-07T10:12:19.847 回答
0

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

终于搞清楚是什么问题了,分享给大家!

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

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

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

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

于 2015-03-08T13:35:33.327 回答