[英]How to use the POST method of JQuery in WP7's WebBrowser control
[英]How can I use BlobstoreUploadHandler with jQuery's post method
我正在嘗試使用以下代碼將文件上傳到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);
}
但是,blobstoreUploadHandler無法讀取此內容:
class Upload(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads()
blob_info = upload_files[0]
導致:
blob_info = upload_files[0]
IndexError: list index out of range
使用webapp2的處理程序,相同的javascript代碼也可以正常工作。
嘗試將文件字段的name
屬性傳遞給get_uploads
方法,如下所示:
的HTML
<input type="file" name="file">
蟒蛇
... = get_uploads('file')
另一種可能性是您沒有創建成功完成上傳所需的blob-key
。 用我自己的注釋形成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.