python – AppEngine BlobStore上传失败,请求在开发环境中有效

我有一个AppEngine应用程序,它使用blobstore存储用户提供的图像数据.当我从Chrome中的表单上传图像到该应用程序时,它工作正常.当我尝试从Android应用程序上传图像时,它会失败.如果我针对开发服务器运行,这两种方法都可以正常工作,但Android上传对现场服务不起作用.

这是来自Chrome的请求:

POST /_ah/upload/?userToken=11001/AMmfu6ZCyMQQ9YdiXal3SmSXIRTQIuSRXkNc-i3JmU0fqx_kJbUJ2OMLcS2lXhVJSK4qs7regViTKzOPz5ejoZYi0nAD5o8vNltiOViQw6DZO7_byZz3Ut0/ALBNUaYAAAAAS_lusgPMAGmpPrg0BuNsJyymX-57ob4i/ HTTP/1.1
Host: photohuntservice.appspot.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.1.249.1064 Safari/532.5
Referer: http://photohuntservice.appspot.com/debug_newpuzzle?userToken=11001
Content-Length: 60360
Cache-Control: max-age=0
Origin: http://photohuntservice.appspot.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybl05YLmLbFRf2MzN
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

------WebKitFormBoundarybl05YLmLbFRf2MzN
Content-Disposition: form-data; name="userToken"

11001
------WebKitFormBoundarybl05YLmLbFRf2MzN
Content-Disposition: form-data; name="img"; filename="Photo_020908_001.jpg"
Content-Type: image/jpeg

<image data>
------WebKitFormBoundarybl05YLmLbFRf2MzN
Content-Disposition: form-data; name="longitude"

-122.084095
------WebKitFormBoundarybl05YLmLbFRf2MzN
Content-Disposition: form-data; name="latitude"

37.422006
------WebKitFormBoundarybl05YLmLbFRf2MzN--

这是我的客户端的请求(在Android上用Java编写,但我不认为这是相关的):

POST /_ah/upload/?userToken=11001/AMmfu6Zf9an6AU4lT9UuhIpxOZyOYb1LMwimFpeSh8zr6J1sX9F2ddJW3Qlsw0kwV3oALv-TNPWRQ6g4_Dgwk0UTwF47bbc78Yl44kDeV69MydTuR3N46S4/ALBNUaYAAAAAS_mMr3CYqTg3aVBDjhRxP0DyyRdvotyG/ HTTP/1.1
Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryhdyNAhmOouRDGErG
Cache-Control: max-age=0
Accept: */*
Origin: http://photohuntservice.appspot.com
Connection: keep-alive
Referer: http://photohuntservice.appspot.com/getuploadurl?userToken=11001
Content-Length: 2638
Host: photohuntservice.appspot.com
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue

------WebKitFormBoundaryhdyNAhmOouRDGErG
Content-Disposition: form-data; name="userToken"

11001
------WebKitFormBoundaryhdyNAhmOouRDGErG
Content-Disposition: form-data; name="img";filename="PhotoHunt.jpg"
Content-Type: image/jpeg

<image data>
------WebKitFormBoundaryhdyNAhmOouRDGErG
Content-Disposition: form-data; name="latitude"

37.422006
------WebKitFormBoundaryhdyNAhmOouRDGErG
Content-Disposition: form-data; name="longitude"

-122.084095
------WebKitFormBoundaryhdyNAhmOouRDGErG--

在这两种情况下,用于捕获请求的AppEngine Python代码都是相同的:

  class UploadPuzzle( blobstore_handlers.BlobstoreUploadHandler ):
        def post(self):
            upload_files = self.get_uploads(  )

问题是当在生产AppEngine服务上运行时,self.get_uploads()在从我的客户端应用程序发出请求时返回一个空列表.这两个请求都会在开发服务器上返回我期望的内容(其中包含一个blob_info的列表),Chrome会返回我在两种情况下的预期.

最佳答案
结果问题是这一行:

Content-Disposition: form-data; name="img";filename="PhotoHunt.jpg"

它应该是这样的:

Content-Disposition: form-data; name="img"; filename="PhotoHunt.jpg"

生产服务器上的表单数据解析器比开发环境中的表单数据更严格,并且在分号和“filename =”之间需要一个空格

转载注明原文:python – AppEngine BlobStore上传失败,请求在开发环境中有效 - 代码日志