从Django 1.4.3的媒体文件夹下载文件(已上传) [英] Downloading the files(which are uploaded) from media folder in django 1.4.3

查看:115
本文介绍了从Django 1.4.3的媒体文件夹下载文件(已上传)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django设计用于处理往返media文件夹的文件的uploadingdownloading的基本网页

I am using django to design the basic web pages that handles the uploading and downloading of the files to/from the media folder

实际上,文件已成功上传到媒体文件夹中,文件也已成功下载,但underscore作为last charater附加到了文件名,如file_one.pdf_file_two.pdf_file_three.txt_等,

Actually the files are uploaded successfully in to the media folder, also files are downloaded successfully but an underscore is appended to the file_name as a last charater like file_one.pdf_ , file_two.pdf_ , file_three.txt_ etc.,

下面是我的代码

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,该视图功能以表格的形式显示文件总数,旁边还有下载链接每个文件名.

So in the above codes, when a file is uploaded successfully in to media , it will be redirected to files_list.html through files_list view functions which displays the total number of files in the form of table with a download link beside to each file name.

因此,当我们单击下载锚链接时,将通过执行功能download下载适当的文件.

So when we click on the download anchor link the appropriate file will be downloaded by executing the function download .

文件下载成功,但是underscore _追加到文件名的末尾,例如file_one.pdf_file_two.pdf_file_three.txt_等.

So the file is downloading sucessfully , but an underscore _ is appending to the last of the file name like file_one.pdf_ , file_two.pdf_ , file_three.txt_ etc.,.

所以任何人都可以让我知道,我上面的下载功能代码有什么问题,为什么underscore附加到file name上,以及如何从文件名中删除该underscore ...

So can anyone please let me know, what's wrong in my above download function code and why underscore is appending to the file name and how to remove that underscore from the file name...

推荐答案

只需删除文件名后的/.

更改此:

response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(file_name) 

对此:

response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) 

这篇关于从Django 1.4.3的媒体文件夹下载文件(已上传)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆