Python Django如何下载现有文件?
在Django的视图中,我创建了一个pdf文件,并希望下载它。 文件已存在(路径:/app/data/4.pdf),我启动以下命令:Python Django如何下载现有文件?,python,django,view,download,Python,Django,View,Download,在Django的视图中,我创建了一个pdf文件,并希望下载它。 文件已存在(路径:/app/data/4.pdf),我启动以下命令: def download_line(request): if not request.is_ajax() and not request.method == 'GET': raise Http404 try: fs =FileSystemStorage('/app/data') with fs.op
def download_line(request):
if not request.is_ajax() and not request.method == 'GET':
raise Http404
try:
fs =FileSystemStorage('/app/data')
with fs.open('4.pdf') as pdf:
response =HttpResponse(pdf,content_type='application/pdf')
response['Content-Disposition']='attachment; filename="4.pdf"'
except Exception as e:
logger.warning("Download Line | Erreur : " + e.message)
return response
但是下载没有启动,也没有错误。你有解决办法吗?
谢谢。试试这个,我用这行代码下载文件
from django.http import HttpResponse
from wsgiref.util import FileWrapper
import os
def download(request, file_path):
"""
e.g.: file_path = '/tmp/file.pdf'
"""
try:
wrapper = FileWrapper(open(file_path, 'rb'))
response = HttpResponse(wrapper, content_type='application/force-download')
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
except Exception as e:
return None
试试这个,我用这行代码下载文件
from django.http import HttpResponse
from wsgiref.util import FileWrapper
import os
def download(request, file_path):
"""
e.g.: file_path = '/tmp/file.pdf'
"""
try:
wrapper = FileWrapper(open(file_path, 'rb'))
response = HttpResponse(wrapper, content_type='application/force-download')
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
except Exception as e:
return None
当文件已经存在时,我用来提供文件下载服务。FileResponse自Django 1.7.4以来一直存在
from django.core.files.storage import FileSystemStorage
from django.http import FileResponse
def download_line(request):
fs = FileSystemStorage('/absolute/folder/name')
FileResponse(fs.open('filename.pdf', 'rb'), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="filename.pdf"'
return response
当文件已经存在时,我用来提供文件下载服务。FileResponse自Django 1.7.4以来一直存在
from django.core.files.storage import FileSystemStorage
from django.http import FileResponse
def download_line(request):
fs = FileSystemStorage('/absolute/folder/name')
FileResponse(fs.open('filename.pdf', 'rb'), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="filename.pdf"'
return response
您可以通过链接和静态链接下载应用程序中的现有文件,如下所示
<a href="{% static 'questions/import_files/import_questions.xlsx' %}" download>Excel Format File </a>
您可以通过链接和静态链接下载应用程序中的现有文件,如下所示
<a href="{% static 'questions/import_files/import_questions.xlsx' %}" download>Excel Format File </a>
也许您需要调用pdf(文件)对象上的read()
方法。i、 e.HttpResponse(pdf.read(),cont.。
不,它不起作用,我应该在javascript端下载?似乎你想使用Ajax提供文件,这是不可能的:好的,我不能使用Ajax从服务器下载文件,我如何在javascript端做到这一点?只需使用也许你需要调用read()
pdf(File)对象上的方法。即HttpResponse(pdf.read()),cont.
不,它不起作用,我应该在javascript端下载?似乎你想使用Ajax提供文件,这是不可能的:好的,我不能使用Ajax从服务器下载文件,我如何在javascript端做到这一点?只需使用它不起作用,错误“view paquet.views.download没有返回HttpResponse对象。相反,它没有返回任何对象”,pb似乎是包装器时出现的此错误发生在文件路径
为incorrect@smiss根据示例,该函数需要C:\tmp\file.pdf
(Windows)或/tmp/file.pdf
(Linux)中的文件它不起作用,错误“view paquet.views.download没有返回HttpResponse对象。相反,它没有返回任何对象”,pb似乎是在包装器时此错误发生在文件路径
为incorrect@smiss根据示例,函数需要C:\tmp\file.pdf
(Windows)或/tmp/file.pdf
(Linux)