Downloading the files(which are uploaded) from media folder in django 1.4.3

蹲街弑〆低调 提交于 2020-01-21 03:05:06

问题


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

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

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>

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.

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

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

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


回答1:


Your code is right but there is one redundant character in download:

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

At last line the filename attribute has a trailing slash (/): filename=%s/

Which causes the problem. Remove this slash and it works.




回答2:


Just remove / after filename.

Change this:

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

to this:

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



回答3:


I solved the problem by replacing

response['Content-Disposition'] = 'attachment; filename=diploma_"' + str(someID) + '.pdf"'

with

response['Content-Disposition'] = 'attachment; filename="diploma_{}{}"'.format(str(someID),'.pdf')



回答4:


import urllib, mimetypes
from django.http import HttpResponse, Http404, StreamingHttpResponse, FileResponse
import os
from django.conf import settings
from wsgiref.util import FileWrapper

class DownloadFileView(django_views):
    def get(self,request,file_name):
        file_path = settings.MEDIA_ROOT +'/'+ file_name
        file_wrapper = FileWrapper(open(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/' % str(file_name) 
        return response


来源:https://stackoverflow.com/questions/15246661/downloading-the-fileswhich-are-uploaded-from-media-folder-in-django-1-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!