I am using django to design basic web pages that handle file uploading and downloading to/from the media folder. The files are successfully uploaded to the media folder and downloaded, but an underscore is added to the end of the file name, such as "file_one.pdf_", "file_two.pdf_", "file_three.txt_", etc.
Below are my codes.
在上述代码中,当文件成功上传到媒体中时,它将通过
因此,当我们单击下载锚链接时,将通过执行
因此,文件正在成功下载,但是在文件名的末尾添加了下划线
因此,请问有谁能告诉我,我的下载函数代码有什么问题,为什么会在文件名后面添加下划线,并且如何从文件名中删除该下划线...
Below are my codes.
urls.py
urlpatterns = patterns('',
url(r'^upload$', 'learn_django.views.upload'),
url(r'^files_list$', 'learn_django.views.files_list'),
url(r'^download/(?P<file_name>.+)$', 'learn_django.views.download'),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + urlpatterns
views.py
def upload(request):
......
....
return render_to_response('uploads_form.html', {'form': form},context_instance=RequestContext(request))
def files_list(request):
return render_to_response('files_list.html',{'total_files':os.listdir(settings.MEDIA_ROOT),'path':settings.MEDIA_ROOT},context_instance=RequestContext(request))
def download(request,file_name):
file_path = settings.MEDIA_ROOT +'/'+ file_name
file_wrapper = FileWrapper(file(file_path,'rb'))
file_mimetype = mimetypes.guess_type(file_path)
response = HttpResponse(file_wrapper, content_type=file_mimetype )
response['X-Sendfile'] = file_path
response['Content-Length'] = os.stat(file_path).st_size
response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(file_name)
return response
files_list.html
<table border="1" colspan="2" width="100%">
<tr>
<th width="60%">File</td>
<th width="40%">Download</td>
</tr>
{% for file in total_files %}
<tr>
<td width="60%">{{file}}</td>
<td width="40%" align="center"><a href="/download/{{file}}" style="text-decoration:None">Download here</a></td>
</tr>
{% endfor %}
</table>
在上述代码中,当文件成功上传到媒体中时,它将通过
files_list
视图函数重定向到files_list.html
,该函数以表格形式显示文件的总数,并在每个文件名旁边提供下载链接。因此,当我们单击下载锚链接时,将通过执行
download
函数下载相应的文件。因此,文件正在成功下载,但是在文件名的末尾添加了下划线
_
,例如file_one.pdf_
,file_two.pdf_
,file_three.txt_
等。因此,请问有谁能告诉我,我的下载函数代码有什么问题,为什么会在文件名后面添加下划线,并且如何从文件名中删除该下划线...