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

14,107

Solution 1

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) 

Solution 2

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.

Solution 3

These are all not required.In HTML you can download the media file by using <a download="{video.URL}">

For example:

<button class="btn btn-outline-info">
<a href="{{result.products.full_video.url}}" download="{{result.products.full_video.url}}" style="text-decoration:None" class="footer_link">Download<i class="fa fa-download"></i></a>
</button>
Share:
14,107
Shiva Krishna Bavandla
Author by

Shiva Krishna Bavandla

I love to work on python and django using jquery and ajax.

Updated on June 05, 2022

Comments

  • Shiva Krishna Bavandla
    Shiva Krishna Bavandla about 2 years

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

  • 4b0
    4b0 over 4 years
    Welcome to Stack Overflow! Code-only answers are not particularly helpful. Please include a brief description of how this code solves the problem.