我正在使用Valumns File Uploader将文件加载到django。这支持通过XHR
为现代浏览器上传ajax,为IE等旧版本支持iFrame后备。
现在的问题是:如果我上传大文件,只有iFrame上传工作,因为这里的文件被映射到request.FILES
,而django立即将它们写入磁盘而不是内存。
如果使用XHR
上传,我必须阅读request._raw_post_data并将其写入磁盘,但它失败并显示MemoryError
。
这是我的文件上传器初始化:
var uploader = new qq.FileUploader({
'action': uploadUrl,
'multiple': true,
'allowedExtensions': allowedExtensions,
'element': selector,
'debug': true,
'onComplete': completeFunction,
'onProgress': progressFunction,
'onSubmit': submitFunction
});
在django端,我使用以下代码将文件内容写入磁盘:
# open a new file to write the contents into
new_file_name = str(uuid.uuid4())
destination = open(upload_path + new_file_name, 'wb+')
# differentiate between xhr (Chrome, FF) and pseudo form uploads (IE)
if len(request.FILES) > 0: # IE
for chunk in request.FILES[0].chunks():
destination.write(chunk)
else: # others
destination.write(request._raw_post_data)
destination.close()
我也尝试了this solution from Alex Kuhl,但这失败了IOError: request data read error
。
有没有办法让XHR
上传的文件进入request.FILES
,所以使用django内置处理?