无法将文件上传到App Engine“列表索引超出范围” [英] Can't upload file to App Engine "list index out of range"
问题描述
我试图从Phonegap应用程序上传图片到我的Python App Engine项目。手机应用程序尝试上传文件,但App引擎返回列表索引超出范围错误。
I am trying to updload a picture from a Phonegap application to my Python App Engine project. The phone app trys to upload the file but App engine returns a "list index out of range" error.
我在App Engine端有以下代码来处理文件,
I have the following code on the App Engine side to handle the files,
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())
And the following code on the PhoneGap side to send the file,
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
当我尝试通过Web表单上传文件时它很好用。
When I try to upload a file via a web form it works great.
推荐答案
您需要通过生成上传url blobstore.create _upload_url( /上传)
。现在你直接制作一个多部分的POST,你的上传处理程序url。中间url是非常重要的,因为它会生成一个Blobstore密钥,并在将它传递给处理程序之前将其添加到MIME头。
You need to generate the upload url via blobstore.create_upload_url('/upload')
. Right now you are making a multi-part POST directly your upload handler url. The intermediate url is critical because it generates a Blobstore key and adds it to the MIME header before passing it to the handler.
这篇关于无法将文件上传到App Engine“列表索引超出范围”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!