V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
kenneth
V2EX  ›  Tornado

Tornado为什么无法上传文件?

  •  
  •   kenneth · 2012-12-06 11:39:53 +08:00 · 4056 次点击
    这是一个创建于 4152 天前的主题,其中的信息可能已经有所发展或是发生改变。
    高手帮忙看下原因?
    class UploadHandler(tornado.web.RequestHandler):
    def post(self):
    file1 = self.request.files['file1'][0]
    original_fname = file1['filename']
    extension = os.path.splitext(original_fname)[1]
    fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6))
    final_filename= fname+extension
    output_file = open("uploads/" + final_filename, 'w')
    output_file.write(file1['body'])
    self.finish("file" + final_filename + " is uploaded")


    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.post1-py2.7.egg/tornado/web.py", line 1064, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
    File "run.py", line 137, in post
    file1 = self.request.files['file1'][0]
    KeyError: 'file1'
    10 条回复    1970-01-01 08:00:00 +08:00
    Livid
        1
    Livid  
    MOD
       2012-12-06 11:44:00 +08:00
    你的提交页面的 HTML 发上来看看。
    binux
        2
    binux  
       2012-12-06 11:44:04 +08:00
    上传表单是怎么样的
    kenneth
        3
    kenneth  
    OP
       2012-12-06 11:54:20 +08:00
    @livid 是不是self.request里面没有东西啊?

    {% extends "base.html" %}
    {% block title %}{% endblock %}
    {% block head %}
    {{ super() }}
    {% endblock %}
    {% block content %}
    <form action="/upload" enctype="mutilpart/form-data" method="post">
    <input type="file" name="file1" />
    <input type="submit" value="上传" />
    {{xsrf_form_html()}}
    </form>
    {% endblock %}
    kenneth
        4
    kenneth  
    OP
       2012-12-06 12:41:35 +08:00
    @livid @binux
    大家帮我分析看看,怎么就报错呢?
    yujnln
        5
    yujnln  
       2012-12-06 12:46:43 +08:00
    你没有定义你的file1
    <input type="file" name="file1" />
    yujnln
        6
    yujnln  
       2012-12-06 12:47:56 +08:00
    错了,是这个 enctype="multipart/form-data"
    kenneth
        7
    kenneth  
    OP
       2012-12-06 12:48:31 +08:00
    @yujnln 什么叫做没有定义file1,不是选择一本本地文件,然后点上传?
    yujnln
        8
    yujnln  
       2012-12-06 12:50:07 +08:00   ❤️ 1
    @kenneth

    是我看得太快了,的确定义了file1
    错误在于 mutilpart 拼写错误,正确是multipart
    你再试试?
    kenneth
        9
    kenneth  
    OP
       2012-12-06 12:50:09 +08:00
    @yujnln 没明白啥意思,能否说清楚一些,错在哪里?
    kenneth
        10
    kenneth  
    OP
       2012-12-06 12:56:26 +08:00
    感谢已发送,真是丢人。我面壁去了。谢谢各位。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   927 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.