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

為{幸葍}努か 提交于 2019-11-30 13:41:51

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.

goromlagche

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) 
Edd

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