无法上传文件到App Engine "列表索引超出范围
我正在尝试从一个Phonegap应用程序上传一张图片到我的Python App Engine项目。手机应用程序试图上传文件,但App Engine返回了一个“列表索引超出范围”的错误。
在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
我对PhoneGap一点都不了解(它是什么呢?)不过那段Python代码是期待你的文件通过HTTP POST方式发送,并且要附在一个叫做file
的字段上。看起来,PhoneGap并没有把文件发送到这个字段里。
4
你需要通过 blobstore.create_upload_url('/upload')
来生成上传的链接。现在你直接把多部分的数据POST到你的上传处理程序的链接上。这个中间的链接非常重要,因为它会生成一个Blobstore的关键字,并在把数据传给处理程序之前,把这个关键字加到MIME头里。