Django 如何使用python将pdf文件下载到本地计算机

Django 如何使用python将pdf文件下载到本地计算机,django,python-2.7,ftp,Django,Python 2.7,Ftp,我已经使用Django 1.11创建了web应用程序,我需要通过FTP或HTTP将文件从使用python的浏览器的应用程序下载到本地系统 HTML代码: ..... {% block content %} {% csrf_token %} <div> <button type="submit" onclick="download_payslip(10)">Download</button> </div> {% endblock content

我已经使用Django 1.11创建了web应用程序,我需要通过FTP或HTTP将文件从使用python的浏览器的应用程序下载到本地系统

HTML代码:

.....
{% block content %}
{% csrf_token %}
<div>
  <button type="submit" onclick="download_payslip(10)">Download</button>
</div>
{% endblock content %}
.....
观点:

def download_payslip(request):
    file_path = "/home/ubuntu/hrmngmt/hrmngmt/static/myfile.pdf"
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/pdf")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            return response
    raise Http404

谢谢您的帮助

谢谢,但它正在运行python代码的服务器中下载,我需要下载到本地系统。我的应用程序运行的服务器中已经有PDF文件,该PDF文件应该下载到我的本地系统,使用应用程序是使用Django 1.11创建的。在服务器上存储文件可能会导致存储问题:我编辑了我的问题,在服务器响应后,我可以在控制台中看到响应数据,但我没有;I don’我不知道如何转换成pdf,你能帮忙吗me@Exprator,它不工作,在服务器上我得到响应“HTTP/1.1200”,但是,当它到达javascript时,在ajax函数中任何需要添加的代码。通过这个链接,您就会理解
import os
from django.conf import settings
from django.http import HttpResponse

def download(request, path):
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/pdf")
            response['Content-Disposition'] = 'inline; filename='payslip.pdf'
            return response
    raise Http404
def download_payslip(request):
    file_path = "/home/ubuntu/hrmngmt/hrmngmt/static/myfile.pdf"
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/pdf")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            return response
    raise Http404
import os
from django.conf import settings
from django.http import HttpResponse

def download(request, path):
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/pdf")
            response['Content-Disposition'] = 'inline; filename='payslip.pdf'
            return response
    raise Http404