【问题标题】:Webapp2 request application/octet-stream file uploadWebapp2 请求应用程序/八位字节流文件上传
【发布时间】:2012-11-17 22:49:25
【问题描述】:

我正在尝试通过 ajax 将文件从表单上传到 s3。我在客户端使用fineuploader http://fineuploader.com/,在服务器端使用webapp2。它在请求中将参数作为 qqfile 发送,我可以在请求标头中看到图像数据,但我不知道如何在不使用多部分编码的浏览器中将其取回。

这就是我在标准 html 表单中使用 multipart 的方式。

image = self.request.POST["image"]

这给了我图像名称和图像文件

目前我只能取回图像文件名而不是数据

image = self.request.get_all('image')
[u'image_name.png']

当使用 POST 时,我收到关于内容标头为 application/octet-stream 的警告

<NoVars: Not an HTML form submission (Content-Type: application/octet-stream)>

如何在 GAE 之外的 webapp2 中实现 BlobStoreHandler?

【问题讨论】:

    标签: python file-upload content-type webapp2


    【解决方案1】:

    我最终使用了fineuploader http://fineuploader.com/,它将一个多部分编码的表单发送到我的处理程序端点。

    在处理程序内部,我可以简单地引用 POST,然后将 FieldStorage 对象读入 cStringIO 对象。

    image = self.request.POST["qqfile"]
    imgObj = cStringIO.StringIO(image.file.read())
    
    # Connect to S3...
    # create s3 Key 
    key = bucket.new_key("%s" % uuid.uuid4());
    
    # guess mimetype for headers
    file_mime_type = mimetypes.guess_type(image.filename)
    if file_mime_type[0]:
       file_headers = {"Content-Type": "%s" % file_mime_type[0]}
    else:
       file_headers = None
    
    key.set_contents_from_string(imgObj.getvalue(), headers=file_headers)
    
    key_str = key.key
    
    #return JSON response with key and append to s3 url on front end.
    

    注意:qqfile 是fineuploader 使用的参数。

    我在伪造进度,但这对于我的用例来说没问题,不需要 BlobStoreUploadHandler。

    【讨论】:

      【解决方案2】:

      您的问题代码对我来说不是很清楚。但是你可以举个例子。

      看看尼克约翰逊的这篇文章。他在客户端使用应用引擎 blobstore 和 Plupload 实现了一个 Dropbox 服务:http://blog.notdot.net/2010/04/Implementing-a-dropbox-service-with-the-Blobstore-API-part-2

      【讨论】:

      • 我的问题是如何使用 webapp2 处理八位字节流请求
      猜你喜欢
      • 2022-10-11
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2019-08-24
      • 2018-08-16
      • 1970-01-01
      • 2019-05-13
      相关资源
      最近更新 更多