如何下载使用django-filebrowser上传的文件?
发布时间:2020-12-20 13:29:22 所属栏目:Python 来源:网络整理
导读:我正在尝试创建一个文件对象的下载.该文件是使用 django-filebrowser添加的,这意味着它将转入该文件的字符串路径.我尝试过以下方法: f = Obj.objects.get(id=obj_id)myfile = FileObject(os.path.join(MEDIA_ROOT,f.Audio.path))...response = HttpResponse
我正在尝试创建一个文件对象的下载.该文件是使用
django-filebrowser添加的,这意味着它将转入该文件的字符串路径.我尝试过以下方法:
f = Obj.objects.get(id=obj_id) myfile = FileObject(os.path.join(MEDIA_ROOT,f.Audio.path)) ... response = HttpResponse(myfile,content_type="audio/mpeg") response['Content-Disposition'] = 'attachment; filename=myfile.mp3' return response 下载的文件包含文件位置的路径字符串,而不是文件.任何人都可以帮助如何访问文件对象? 解决方法f = Obj.objects.get(id=obj_id) myfile = open(os.path.join(MEDIA_ROOT,f.Audio.path)).read() ... response = HttpResponse(myfile,content_type="audio/mpeg") response['Content-Disposition'] = 'attachment; filename=myfile.mp3' return response 注意!这不是内存友好的!由于整个文件被放入内存.您最好使用网络服务器进行文件服务,或者如果您想使用Django进行文件服务,可以使用xsendfile或查看此thread (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |