我已编写代码在Django上传文件,如下所示:
def upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return render_to_response('uploadsuccess.html')
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form})
def handle_uploaded_file(f):
filename = "/media/Data/static/Data/" + f.name
destination = open(filename, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
代码对我来说很好。但是,我不知道如何修改此代码以在客户端显示进度条。
我的html页面如下:
{% extends "index_base.html" %}
{% block content %}
<script src="/media/js/functions.js" type="text/javascript"></script>
<script src="/media/js/jquery.js" type="text/javascript">
</script>
<div id="main_container">
{% include "includes/nav.html" %}
<!------- Main Contents ---------->
<div id="contents_holder">
<div id="contents">
<div id="c_banner">
<span class="main_title">Upload File</span>
</div>
<div id="setting">
<form name="post" action="/upload.psp/" method="post" enctype="multipart/form-data">
<h2>Upload File</h2></br>
<p>{{ form.file.label_tag }} {{ form.file }}</p></br>
<input type="submit" value="Upload" name="uploadButton" />
<input type="button" value="Cancel" name="cancelUploadButton" onclick ="cancelUploadClicked()"/>
<input type="hidden" value="title" name="title" id="title" />
</form>
</div>
</div>
</div>
</div>
<!------- Main Contents Finished ---------->
{% endblock %}
有人可以帮我整合上传进度条和这段代码吗?
提前致谢。
答案 0 :(得分:0)
我认为您必须使用Ajax发送异步请求。
这里还讨论了这个主题:File upload progress bar with jquery 在这里:How can I upload files asynchronously?