无法将文件上传到App Engine“列表索引超出范围”

时间:2011-08-08 21:03:34

标签: python google-app-engine cordova

我正在尝试将图片从Phonegap应用程序更新到我的Python App Engine项目。手机应用程序尝试上传文件,但App引擎返回“列表索引超出范围”错误。

我在App Engine端有以下代码来处理文件,

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
    upload_files = self.get_uploads('file')    
    blob_info = upload_files[0]        
    self.redirect('/serve/%s' % blob_info.key())

以下代码在PhoneGap端发送文件,

    function uploadImage() {
        var smallImage = document.getElementById('cameraImage');
        if (smallImage.src && smallImage.src !== "") {
            var f = new FileTransfer();
            f.upload(smallImage.src, "http://testtest.appspot.com/upload",
                // success callback
                function(result) {
                    document.getElementById('uploadProgress').innerHTML =
                        result.bytesSent + ' bytes sent';
                    alert(result.responseCode + ": " + result.response);
                },
                // error callback
                function(error) {
                    alert('error uploading file: ' + error.code);
                },
                // options
                { fileName: 'myImage.jpg', 
                  params: { 'username':'jtyberg' } 
                });
        }
    }

错误如下,

"POST /upload HTTP/1.1" 500 487 - "BlackBerry9550/5.0.0.469 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/-1" "testtest.appspot.com" ms=416 cpu_ms=93 api_cpu_ms=0 cpm_usd=0.014221 loading_request=1 instance=00c61b117ca6c4ea405471eea592a8f79ac6
E 2011-08-06 11:49:17.309
list index out of range
Traceback (most recent call last):
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 702, in __call__
        handler.post(*groups)
    File "/base/data/home/apps/s~testtest/1.352363227571120815/main.py", line 62, in post
        blob_info = upload_files[0]
IndexError: list index out of range

当我尝试通过网络表单上传文件时,效果很好。

2 个答案:

答案 0 :(得分:4)

您需要通过blobstore.create_upload_url('/upload')生成上传网址。现在,您直接在上传处理程序URL上进行多部分POST。中间URL非常重要,因为它会生成一个Blobstore密钥,并在将其传递给处理程序之前将其添加到MIME标头中。

答案 1 :(得分:0)

我对PhoneGap一无所知(这是什么?)但Python代码期望您的文件通过附加到名为file的字段的HTTP POST发送。据推测,PhoneGap并没有针对该领域发送它。