我們都知道,App Engine將大多數輸入/輸出請求限制爲1 MB。但是通過最近的BlobStore API,您可以通過發佈到動態生成的URL來完整地上傳大文件。如何使用XmlHttpRequest 2.0將5 MB文件上傳到App Engine BlobStore?
根據樣本,這裏是HTML形式會是什麼樣子:
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST"
enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File:
<input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit">
</form></body></html>""")
但是我們該怎麼辦這個異步使用JavaScript技術與HTML5出臺?這是我到目前爲止有一個片段:
xhr.open("POST", post_url); // the post URL given by App Engine
xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('X-File-Name', file.fileName);
// After loading the binary data (last time we only read as base64 string)
// Tell xhr to start the upload
myBinaryDataReader.addEventListener("loadend", function(evt){
xhr.sendAsBinary(evt.target.result);
}, false);
// Initiate the binary reading on the file, when finished it will
// upload asynchronously
myBinaryDataReader.readAsBinaryString(file);
你會發現,這一技術將原始的二進制文件作爲POST
體。這很好,它可以在不需要BlobStore的情況下工作達1 MB。在Python中,讀取該文件,我只是用:
img_data = self.request.body # got my image data now
然而,隨着BLOBSTORE,我應該使用
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
但我不會使用與輸入型HTML表單= file,我正在使用XmlHttpRequest - 我如何讓App Engine「認爲」它是HTML表單中的文件,從而「抓取」文件數據?
我的代碼,未修改,導致錯誤
File "C:\Python26\lib\cgi.py", line 583, in keys
raise TypeError, "not indexable"
TypeError: not indexable
或者,有沒有一種方法可以將原始POST正文放入App Engine的BlobStore中? – 2010-08-31 02:04:41