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

I use django to create basic web pages that process files uploadingand downloadingfor files to / from a foldermedia

In fact, the files are loaded successfully in the media folder and files are downloaded successfully, but it underscoreis added to the file name as the last charaterlike 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 the file is successfully downloaded to the media, it will be redirected to the function files_list.htmlthrough the files_listviewer, which displays the total number of files in the form of a table with a download link next to each file name.

, , , download.

, , underscore _ , file_one.pdf_, file_two.pdf_, file_three.txt_ ..,

-, , , underscore file name underscore ...

+7
3

/ .

:

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

:

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

, 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

filename (/): filename=%s /

. .

+5

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')
0
source

All Articles