【问题标题】:Download file served as response下载文件作为响应
【发布时间】:2014-07-18 13:43:07
【问题描述】:

我正在尝试实现以下目标:在客户端,使用 Jquery/Ajax,我向我的 Django 服务器发出 Post 请求。在服务器端,我收到此请求,提取参数,基于此确定文件路径,然后构建 HttpResponse ,目的是让浏览器下载该文件。

但这并没有发生,尽管我在响应中看到了附件的内容。

响应构建为:

 response = HttpResponse(file(path_to_file))
 response['Content-Type'] = 'application/force-download'
 response['Content-Length'] = os.path.getsize(path_to_file)
 response['Content-Disposition'] = 'attachment; filename=\"request.txt\"'
 response['Accept-Ranges'] = 'bytes'
 return response

这是使用 firebug 看到的响应标头

Accept-Ranges   bytes
Connection  keep-alive
Content-Disposition attachment; filename=grequest.txt
Content-Length  228
Content-Type    application/force-download
Date    Fri, 18 Jul 2014 14:55:33 GMT
Server  nginx/1.4.4
Set-Cookie  sessionid=41602cd107bbddb41e8884a88c9035c0; Path=/
Vary    Authorization, Cookie

这是响应内容,用萤火虫看到

eyJub2RlSUQiOiIwMmI1ODMtYjNhMTljLWM1MjkwYi05YzAwIiwiYWxpYXMiOiJsb2NhbGhvc3QiLCJkbnNOYW1lIjoibG9jYWxob3N0IiwiY2hhc3Npc1NlcmlhbCI6IiIsImFjdGl2YXRpb25zIjpbeyJhY3RpdmF0aW9uSUQiOiI3RjE3LUZFRUQtMTI5Ny1GOTUyIiwicXVhbnRpdHkiOiIzIn1dfQ==

这里是附件的内容

关于我做错了什么有什么建议吗?

【问题讨论】:

    标签: jquery ajax django


    【解决方案1】:

    试试

    html

    <a id="download" download="" href="">download</a>
    

    js

    $(function() {
        var request = function (url, filename) {
         /* var file = {json : JSON.stringify([ "eyJub2RlSUQiOiIwMmI1ODMtYjNhMTljLWM1MjkwYi05YzAwIiwiYWxpYXMiOiJsb2NhbGhvc3QiLCJkbnNOYW1lIjoibG9jYWxob3N0IiwiY2hhc3Npc1NlcmlhbCI6IiIsImFjdGl2YXRpb25zIjpbeyJhY3RpdmF0aW9uSUQiOiI3RjE3LUZFRUQtMTI5Ny1GOTUyIiwicXVhbnRpdHkiOiIzIn1dfQ=="])}; */
        $.ajax({
            beforeSend : function(jqxhr, settings) {
                jqxhr.filename = filename;
            },
            url : url,
            type : "POST",
            dataType : "text json",
            /* data : file, */
            success : function(data, textStatus, jqxhr) {
            $("a#download").attr({
                "download" : jqxhr.filename,
                "href" : "data:text/plain;base64," + data /* data[0] */       
            }).get(0).click();
            }
        });
        };
        request("/echo/json/", "request.txt")
    });
    

    jsfiddle http://jsfiddle.net/guest271314/Xd4zk/

    【讨论】:

      【解决方案2】:

      对您的代码进行一些调整。试试这个...

      response = HttpResponse(file(path_to_file).read(), mimetype='application/force-download')
      

      另外,为什么要转义

      中的双引号
      response['Content-Disposition'] = 'attachment; filename=\"request.txt\"'
      

      【讨论】:

      • 添加了建议的更改,但行为保持不变。
      • 尝试不使用mimetypeContent-Type。至少文件应该显示在浏览器中。如果没有发生这种情况,请检查该文件是否确实存在。并在 shell 中尝试.read()
      【解决方案3】:

      您无法使用 Ajax 从服务器下载文件。出于安全原因,Javascript 不允许与客户端的硬盘交互,因此无法将响应保存到磁盘。

      常见的解决方法是包含隐藏 iframe 并使用 Javascript 设置 src 属性,或使用 Javascript 设置 window.location.href。这两种方法都应该在不离开当前页面的情况下通过保存对话框提示用户。

      【讨论】:

      • 能不能加几行代码更清楚?
      • @steve 网上有很多例子,他们比我更了解 html/javascript。我建议您搜索“隐藏 iframe 下载”或“窗口位置下载”。
      • 谢谢,我会继续调查这个
      猜你喜欢
      • 1970-01-01
      • 2013-06-15
      • 2021-03-20
      • 1970-01-01
      • 2019-01-04
      • 2018-03-20
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      相关资源
      最近更新 更多