0

我正在从 django 的管理面板单击链接创建文件下载功能。我正在使用 FileField 来存储文件。出于下载目的,我研究并找到了有关 stackoverflow 的帮助。使用该帮助后,我有以下用于文件下载的代码(我自己的一些小改动)。

def pdf_download(request):
    #print("request: ", request.META["PATH_INFO"])
    a = request.META["PATH_INFO"]
    #print(type(a))
    a = a.split("/")
    a = a[-1]
    #print(a)
    #print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    with open(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+"\\router_specifications\\"+a ,"rb") as pdf:
        #Here router_specifications is the directory on local storage where the uploaded files are being stored.
        response = HttpResponse(pdf.read()) #can add ', content_type = "application/pdf" as a specific pdf parameter'
        response["Content-Disposition"] = "attachment; filename ="+a
        pdf.close()
        return response

现在,当我在笔记本电脑上运行这段代码时,文件会自动下载。但是,当我切换到其他笔记本电脑时,它会询问我应该将文件保存在哪里,即它不会自动下载。我应该做些什么更改才能自动下载文件而不要求手动保存。第一时间寻求帮助。

4

1 回答 1

1

您可以尝试添加以下 content_type:

content_type='应用程序/强制下载'

于 2018-10-26T15:45:02.007 回答