回复['内容 - 处置'] ='附件;'在django没有下载文件

时间:2017-11-08 09:32:58

标签: javascript python django

filename = "/filepath/in/server"
    with open(filename, 'rb') as f:
        response = HttpResponse(f.read(), content_type='application/octet-stream')
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment; filename=filename'
        print response
        return response

单击我的Web应用程序中的按钮应该下载文件并将文件从服务器保存到我的桌面。我的html代码只包含按钮和onclick函数(提交表单并调用POST)。 Post脚本是用views.py编写的。上面的代码也是用views.py编写的,但点击按钮不会将文件作为附件下载。我可以看到服务器端打印的响应内容。

编辑:HTML代码

<form id="downloadFile" name="downloadFile" method="post"> 
  <button id="downloadbtn" name="downloadbtn" onclick="downloadFile(this);"></button>
  {% csrf_token %}
  <input type="hidden" name="csrfmiddlewaretoken17" value="{{ csrf_token }}"/>
  <input id="downloadForm" type="hidden" name="form_type" value="downloadVCMTSRules"/>
</form> 
<script type="text/javascript">
    function downloadFile(activeElement){ 
       document.getElementById("downloadFile").method="POST"; 
       document.getElementById("downloadFile").submit();
    }
</script>

如果我点击按钮,页面就会重新加载。

0 个答案:

没有答案
相关问题