12

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313

5 Answers5

6

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) 
That1Guy
  • 7,075
  • 4
  • 47
  • 59
goromlagche
  • 432
  • 1
  • 5
  • 12
6

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.

CumaTekin
  • 63
  • 1
  • 7
2

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>
4b0
  • 21,981
  • 30
  • 95
  • 142
0

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')
Zoe
  • 27,060
  • 21
  • 118
  • 148
Edd
  • 932
  • 10
  • 24
0
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
4b0
  • 21,981
  • 30
  • 95
  • 142
surya
  • 1
  • 1
    Welcome to Stack Overflow! Code-only answers are not particularly helpful. Please include a brief description of how this code solves the problem. – 4b0 Dec 05 '19 at 11:33