下载文件担任响应文件

2023-09-10 22:16:52 作者:横尸遍野

我想实现以下目标:在客户端,使用jQuery /阿贾克斯,我做一个POST请求我的Django的服务器。在服务器端,我得到这个请求,提取参数的基础上,我判断一个文件路径,然后我建立的Htt presponse,具有浏览器下载该文件的意图。

然而它不用发生,虽然我在响应看到附加的文件的内容。

的反应是建立如下:

 响应= Htt的presponse(文件(文件路径))
 响应['的Content-Type] ='应用/武力下载
 响应['的Content-Length'] = os.path.getsize(文件路径)
 响应['内容处置'] ='附件;文件名= \request.txt \'
 响应['接受-范围'] ='字节'
 返回响应
 

这里的响应头看到的萤火虫

 接受-范围字节
连接保持
内容处置附件;文件名= grequest.txt
内容长度228
内容类型的应用程序/力下载
日期星期五,2014年7月18日十四时55分33秒格林尼治标准​​时间
服务器的nginx / 1.4.4
设置Cookie会话ID = 41602cd107bbddb41e8884a88c9035c0;路径= /
有所不同的授权,饼干
 

和这里的响应内容,看到的萤火虫

eyJub2RlSUQiOiIwMmI1ODMtYjNhMTljLWM1MjkwYi05YzAwIiwiYWxpYXMiOiJsb2NhbGhvc3QiLCJkbnNOYW1lIjoibG9jYWxob3N0IiwiY2hhc3Npc1NlcmlhbCI6IiIsImFjdGl2YXRpb25zIjpbeyJhY3RpdmF0aW9uSUQiOiI3RjE3LUZFRUQtMTI5Ny1GOTUyIiwicXVhbnRpdHkiOiIzIn1dfQ== QQ2010上传不了文件,点了上传文件也没反应,但是下载没问题

在这种情况下,附加文件的内容

这是我做的任何建议错了?

解决方案

尝试

HTML

 <一个ID =下载下载=的href =>下载< / A>
 

JS

  $(函数(){
    VAR请求=功能(URL,文件名){
     / * var文件= {JSON:JSON.stringify([ "eyJub2RlSUQiOiIwMmI1ODMtYjNhMTljLWM1MjkwYi05YzAwIiwiYWxpYXMiOiJsb2NhbGhvc3QiLCJkbnNOYW1lIjoibG9jYWxob3N0IiwiY2hhc3Npc1NlcmlhbCI6IiIsImFjdGl2YXRpb25zIjpbeyJhY3RpdmF0aW9uSUQiOiI3RjE3LUZFRUQtMTI5Ny1GOTUyIiwicXVhbnRpdHkiOiIzIn1dfQ=="])}; * /
    $阿贾克斯({
        beforeSend:功能(jqxhr,设置){
            jqxhr.filename =文件名;
        },
        网址:网址,
        键入:POST,
        数据类型:文字JSON
        / *数据:文件,* /
        成功:功能(数据,textStatus,jqxhr){
        $(A#下载)。ATTR({
            下载:jqxhr.filename,
            HREF:数据:text / plain的; BASE64,+数据/ *数据[0] * /
        })得到(0)。单击()。
        }
    });
    };
    请求(/回声/ JSON /,request.txt)
});
 

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

I'm trying to achieve the following : On the client side ,using Jquery/Ajax , I make a Post request to my Django server. On the server side I get this request, extract parameters ,based on that I determine a file path and then I build a HttpResponse , with the intention of having the browser to download that file.

However this doest not happen, although I see in the response the content of the attached file.

The response is build as :

 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

Here's the Response headers as seen with 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

and here's the response content ,seen with firebug

eyJub2RlSUQiOiIwMmI1ODMtYjNhMTljLWM1MjkwYi05YzAwIiwiYWxpYXMiOiJsb2NhbGhvc3QiLCJkbnNOYW1lIjoibG9jYWxob3N0IiwiY2hhc3Npc1NlcmlhbCI6IiIsImFjdGl2YXRpb25zIjpbeyJhY3RpdmF0aW9uSUQiOiI3RjE3LUZFRUQtMTI5Ny1GOTUyIiwicXVhbnRpdHkiOiIzIn1dfQ==

In this case the content of the attached file

Any suggestion on what I am doing wrong ?

解决方案

Try

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/