【发布时间】:2010-08-31 02:03:29
【问题描述】:
我们都知道,对于大多数输入/输出请求,App Engine 将您限制为 1 MB。但是使用最近的 BlobStore API,您可以通过 POST 到动态生成的 URL 来完整上传大文件。
根据示例,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>""")
但是我们如何使用 HTML5 引入的 JavaScript 技术异步执行此操作?这是我目前所拥有的 sn-p:
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
但我没有使用输入 type=file 的 HTML 表单,我使用的是 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?
标签: python google-app-engine file-upload