12

我正在使用 django 来设计处理文件夹的文件uploadingdownloading文件的基本网页media

实际上,文件已成功上传到媒体文件夹,文件也已成功下载,但文件underscore名后面附加了一个last charaterlike file_one.pdf_file_two.pdf_file_three.txt_

以下是我的代码

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

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

文件列表.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.html通过files_listview 函数,它以表格的形式显示文件的总数,每个文件名旁边都有一个下载链接。

因此,当我们单击下载锚链接时,将通过执行该函数下载相应的文件download

所以文件下载成功,但是underscore _文件名的最后一个附加了一个,如file_one.pdf_file_two.pdf_file_three.txt_

所以谁能告诉我,我上面的下载功能代码有什么问题,为什么underscore要附加到文件名中file name以及如何underscore从文件名中删除它......

4

5 回答 5

6

只需/在文件名后删除。

改变这个:

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

对此:

response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) 
于 2013-12-17T08:05:27.027 回答
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=%s/

这导致了问题。删除这个斜线,它就可以工作了。

于 2016-06-13T10:04:28.747 回答
2

这些都不是必需的。在 HTML 中,您可以使用<a download="{video.URL}">

例如:

<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>
于 2020-03-11T10:31:08.097 回答
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
于 2019-12-05T11:31:50.330 回答
0

我通过更换解决了这个问题

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

response['Content-Disposition'] = 'attachment; filename="diploma_{}{}"'.format(str(someID),'.pdf')
于 2019-06-14T15:02:19.000 回答