Submitted.html JS 也被一个 document.ready 函数包围
var uploader = new qq.FileUploader({
action: "{% url 'QCOMLTE:imager' %}",
element: $('#file-uploader')[0],
multiple: true,
onComplete: function(id, fileName, responseJSON) {
if(responseJSON.success) {
alert("success!");
} else {
alert("upload failed!");
}
},
onAllComplete: function(uploads) {
// uploads is an array of maps
// the maps look like this: {file: FileObject, response: JSONServerResponse}
alert("All complete!");
},
params: {
'csrf_token': "{{ csrf_token }}",
'csrf_name': 'csrfmiddlewaretoken',
'csrf_xname': 'X-CSRFToken'
}
});
html 正文中的其他位置
<div id="file-uploader">
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
</noscript>
</div>
网址.py
urlpatterns = patterns('',
url(r"^Submitted/$", views.HybridDetailView.as_view(), name='Submitted'),
url(r'^(?P<object_type>\w+)/process/$', views.process, name='process'),
url(r'^(?P<object_type>\w+)/$', views.generic_view, name='generic'),
url("$^", views.head, name='head'),
url("uploader", views.upload, name= 'imager'),
)
视图.py
@AjaxFileUploader
def upload(request):
response = {'files': []}
script_dir = os.path.dirname(__file__)
# Loop through our files in the files list uploaded
print('request',request)
print(request.FILES)
for item in request.FILES.getlist('files[]'):
file = UploadedFile(item)
with open(script_dir + '/Excel/' + file.name) as destination:
for chunk in file.chunks():
destination.write(chunk)
response['files'].append(file)
print('appended')
return HttpResponse(json.dumps(response), content_type='application/json')
在已安装的应用程序列表中还包含“ajaxuploader”
当我尝试通过按钮提交文件时,它会发送 post 调用但收到 400 (BAD REQUEST) 错误。据我所知,它甚至没有到达 python,至少不是视图代码。似乎正确地形成了请求 URL
http://localhost:8000/QCOMLTE/uploader?qqfile=powered_by.png
当您访问该 URL 时,它会发送一条消息,说明只允许邮寄电话。这类似于Default django-ajax-uploader with s3 backend 给出 MalformedXML 错误 除了我没有使用任何后端,只是试图抓取文件并将它们保存到目录中。
2014 年 8 月 25 日更新:从视图中删除了装饰器。这会导致错误未发送。打印请求后,很明显文件被发送到 GET 路径而不是 FILE 路径。我不知道如何改变这一点。