[英]How can I use BlobstoreUploadHandler with jQuery's post method
I'm trying to upload a file to GAE's BlobStore using the code: 我正在尝试使用以下代码将文件上传到GAE的BlobStore:
$(function() {
$("input[type=file]").button().change(function( event ) {
var file = document.getElementById('file_load').files[0];
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = shipOff;
});
});
function shipOff(event) {
var result = event.target.result;
var fileName = document.getElementById('file_load').files[0].name;
$.post('{{ upload_url }}', { data: result, name: fileName}, afterSubmission);
}
this however is not read by the blobstoreUploadHandler: 但是,blobstoreUploadHandler无法读取此内容:
class Upload(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads()
blob_info = upload_files[0]
resulting in: 导致:
blob_info = upload_files[0]
IndexError: list index out of range
The same javascript code worked fine using webapp2's handler. 使用webapp2的处理程序,相同的javascript代码也可以正常工作。
Try passing the name
property of the file field to the get_uploads
method like this: 尝试将文件字段的
name
属性传递给get_uploads
方法,如下所示:
HTML 的HTML
<input type="file" name="file">
Python 蟒蛇
... = get_uploads('file')
The other possibility is that you didn't create the blob-key
which is required for the upload to succeed. 另一种可能性是您没有创建成功完成上传所需的
blob-key
。 Form the get_upload
method with my own comments: 用我自己的注释形成
get_upload
方法:
def get_uploads(self, field_name=None):
"""Get uploads sent to this handler.
Args:
field_name: Only select uploads that were sent as a specific field.
Returns:
A list of BlobInfo records corresponding to each upload.
Empty list if there are no blob-info records for field_name.
"""
if self.__uploads is None:
self.__uploads = collections.defaultdict(list)
for key, value in self.request.params.items():
if isinstance(value, cgi.FieldStorage):
if 'blob-key' in value.type_options: # __uploads field is updated if blob-key is present
self.__uploads[key].append(blobstore.parse_blob_info(value))
if field_name:
return list(self.__uploads.get(field_name, []))
else:
results = [] # if not __uploads and not field_name returns [], this is what happened to you
for uploads in self.__uploads.itervalues():
results.extend(uploads)
return results
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.