如何在twisted.web中完成不上传的文件上传? [英] How can I accomplish file uploads in twisted.web that don't suck?

查看:72
本文介绍了如何在twisted.web中完成不上传的文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了搜索,但似乎找不到以任何合理方式将文件上传到twisted.web应用程序的方法.

I've searched and searched but can't seem to find a way to upload files to my twisted.web application in any reasonable way.

当前,将文件上传到资源中会导致产生一个request.args['file']变量,该变量是一个填充了文件内容的列表.我找不到一种方法来获取有关该文件的任何信息:mime类型,文件名,文件大小(不只是在args['file'][]中获取字符串的长度),等等.

Currently, posting file uploads to a resource results in a request.args['file'] variable, that is a list populated with file contents. I can't find a way to get any information about the file: mime type, filename, filesize (other than just taking the length of the strings in args['file'][]), etc.

我已经读过twisted.web2在文件上传方面更胜一筹.但是我不知道有多少好,或者我不知道如何使用twisted.web2在twisted.web应用程序中处理文件上传.

I have read that twisted.web2 is better at file uploads. However I don't know how much better it is, or how I would use twisted.web2 to handle file uploads in a twisted.web application.

有什么建议吗?这让我发疯了-哦,我看着请求标头,却没有真正发现任何有意义的东西.如何获得有关Twisted上传文件的更多元信息?

Any suggestions? This is bugging me like crazy -- Oh and I looked at the request headers, and didn't really find anything of any significance. How can I get some more meta information about file uploads with Twisted?

如何才能从请求对象中获取裸HTTP请求?有可能吗?

How can I just get the bare HTTP request from a request object? Is it possible?

推荐答案

这是一个老问题,但是快速搜索stackoverflow并没有发现类似的问题/答案,因此这里是使用用于文件上传.

This is an old question, but a quick search of stackoverflow didn't turn up a comparable question/answer, so here is a quick example of using twisted.web2 for file uploads.

隐藏的表单变量file_foo与 文件上传变量,以显示Twisted如何将其拆分:

The hidden form variable file_foo shares the same name as a file upload variable, to show how Twisted will split these out:

<form action="/upload?a=1&b=2&b=3" enctype="multipart/form-data"
        method="post">
    <input type="hidden" name="foo" value="bar">
    <input type="hidden" name="file_foo" value="not a file">
    file_foo: <input type="file" name="file_foo"><br/>
    file_foo: <input type="file" name="file_foo"><br/>
    file_bar: <input type="file" name="file_bar"><br/>
    <input type="submit" value="submit">
</form>

在您的Resource.render()方法中,这是您访问表格的方式 变量:

In your Resource.render() method, here's how you could access the form variables:

def render(self, ctx):
    request = iweb.IRequest(ctx)
    for key, vals in request.args.iteritems():
        for val in vals:
            print key, val

    print 'file uploads ----------------'
    for key, records in request.files.iteritems():
        print key
        for record in records:
            name, mime, stream = record
            data = stream.read()
            print '   %s %s %s %r' % (name, mime, stream, data)

    return http.Response(stream='upload complete.')

输出:

         a: 1
         b: 2 3
       foo: bar
  file_foo: not a file

file_bar
   bar.txt MimeType('text', 'plain', {}) <open file '<fdopen>', mode 'w+b' at 0x2158a50> 'bar data.\n\n'
file_foo
   foo.txt MimeType('text', 'plain', {}) <open file '<fdopen>', mode 'w+b' at 0x2158930> 'foo data.\n\n'
   foo.txt MimeType('text', 'plain', {}) <open file '<fdopen>', mode 'w+b' at 0x21589c0> 'foo data.\n\n'

这篇关于如何在twisted.web中完成不上传的文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆