我是Django的新手,我的项目要求我在单击链接时提示用户打开pdf。我的本地计算机上已经有pdf文件,并且不想使用Reportlab重新创建它。有什么办法吗?
我试过了
with open("/user/some/directory/somefilename.pdf") as pdf:
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
return response
但返回的404页面未找到,因为请求的URL不在myproject.urls的URLconf中
我想念什么?
对我有用的方法是使用FileSystemStorage
from django.core.files.storage import FileSystemStorage
from django.http import HttpResponse
fs = FileSystemStorage("/Users/location/where/file/is_saved/")
with fs.open("somefile.pdf") as pdf:
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="my_pdf.pdf"'
return response
现在,它提示用户像平常一样保存文件!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句