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

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

问题描述

我正在使用 django 设计基本的网页,用于处理uploadingdownloading 文件到/从media 文件夹

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

实际上文件已成功上传到媒体文件夹,文件也已成功下载,但下划线作为最后一个字符附加到file_name,如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>

所以在上面的代码中,当一个文件成功上传到 media 时,它将通过显示总数的 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 以及如何删除该 下划线从文件名...

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...

推荐答案

只需删除文件名后的 / 即可.

Just remove / after filename.

改变这个:

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天全站免登陆