如何获取附件文件的内容与jQuery附件、文件、内容、jQuery

2023-09-10 19:09:30 作者:瑾色如弦

在一个 GET 回应我有内容处置:附件;文件名= f.csv ,我需要下载内容在页面上此文件。 在一个 $。阿贾克斯要求我有一个错误。 我怎样才能使用jQuery文件的内容 AJAX (或 GET )? UPD

 错误:函数(jqXHR,textStatus,errorThrown){
  的console.log(jqXHR,textStatus,errorThrown);
}
 

GET

 对象{
    ...
    readyState的0
    responseText的
    状态0
    状态文本错误
}, 错误,
 
html和jquery库文件在同一个文件夹下 怎么在html中写jQuery路径

UPD 2 我发现了一个 jquery.fileDownload 的插件,但它显示了浏览器窗口的保存或打开对话框,如下所示: 但我需要得到的文件内容。 我无需下载电脑上的文件。

UPD 3 全部code房源:

  $。阿贾克斯({
    网址:链接,
    跨域:真正的,
    数据类型:文本,
    成功:功能(数据,textStatus,jqXHR){
        警报(数据);
    },
    错误:函数(jqXHR,textStatus,errorThrown){
        的console.log(jqXHR,textStatus,errorThrown);
    }
});
 

文件生成另一个服务,我不能改变它。 UPD 4 首先I'l的尝试从这样的另一个域获得 JSON 数据:

  $。阿贾克斯({
    网址:链接,
    异步:真正的,
    缓存:真正的,
    数据类型:JSONP
    跨域:真正的,
    键入:GET,
    JSONP:finance_charts_json_callback
    jsonpCallback:finance_charts_json_callback
    错误:函数(jqXHR,textStatus,errorThrown){
        的console.log(jqXHR,textStatus,errorThrown);
    },
    成功:功能(数据,textStatus,jqXHR){
        的console.log(数据);
    }
});
 

链接的样子http://chartapi.finance.yahoo.com/instrument/1.0/a/chartdata;type=quote;ys=2012;yz=2;ts=1234567890/json?finance_charts_json_callback=finance_charts_json_callback

和它的响应报头:

  HTTP / 1.1 200 OK
日期:星期三,二○一四年四月三十零日12时01分08秒格林尼治标准​​时间
P3P:policyref =htt​​p://info.yahoo.com/w3c/p3p.xml,CP =CAO ... GOV
缓存控制:公共
到期日:周四,2014年01 GMT○时32分18秒五月
最后一次修改:星期三,2014年4月30号零时32分十八秒格林尼治标准​​时间
内容类型:text / javascript的;字符集= UTF-8
内容编码:gzip
有所不同:接受编码,X-SSL
年龄:0
途经:HTTP / 1.1 yts39.global.media.ir2.yahoo.com(...)
服务器:ATS
连接:保持活动
 

一切工作正常。

当我尝试从另一台服务器获取文件有它的响应报头:

  HTTP / 1.1 200 OK
缓存控制:无缓存
日期:星期三,二○一四年四月三十〇日12时零九分01秒格林尼治标准​​时间
杂注:无缓存
内容类型:文本/ CSV
到期日:-1
服务器:Microsoft-IIS / 6.0
的X已启动方式:ASP.NET
的X ASPNET-版本:2.0.50727
内容处置:附件;文件名= export.csv
内容编码:gzip
有所不同:接受编码
 

解决方案

编辑,更新

尝试

  $(函数(){
    $ .getJSON(https://query.yahooapis.com/v1/public/yql?q=select
              +*从CSV其中,url =HTTP://finviz.com/export.ashx'
              +&放大器;格式= JSON和放大器;诊断=真放;回调=?
      ,功能(数据,textStatus,jqxhr){
        $每个(data.query.results.row,函数(指数值){
            jqxhr.promise()
            .done(函数(){
                    $(<李>中,{
                      文:value.col2 ++ value.col8
                    })
                    的CSS(填充,8像素)
                    .appendTo(OL);
            })
            。总是(函数(){
                如果(指数=== 4999){
                  的console.log(data.query
                            ,data.query.diagnostics.warning
                            ,data.query.results.row.length
                            , 指数
                            ,$(礼)的长度);
                };
            });
        });
    });
});
 

http://jsfiddle.net/guest271314/yxfEp/

On a get response I have content-disposition: attachment;filename=f.csv and I need to download content of this file on the page . On a $.ajax request I have an error. How can I get content of file using jQuery ajax(or get)? UPD

error: function( jqXHR, textStatus, errorThrown ) {
  console.log( jqXHR, textStatus, errorThrown );
}  

get

Object {
    ...
    readyState 0
    responseText ""
    status 0
    statusText "error"
}, error,  

UPD 2 I found a jquery.fileDownload plugin, but it shows browser's window with save or open dialog, like this: But I need to get file content. I'm no need to download file on computer.

UPD 3 Full code listing:

$.ajax( {
    url: link,
    crossDomain: true,
    dataType: "text",
    success: function( data, textStatus, jqXHR ) {
        alert( data );
    },
    error: function( jqXHR, textStatus, errorThrown ) {
        console.log( jqXHR, textStatus, errorThrown );
    }
} );  

File generates by another service and I can't change it. UPD 4 First of all I'l try to get json data from another domain like this:

$.ajax( {
    url: link,
    async: true,
    cache: true,
    dataType: "jsonp",
    crossDomain: true,
    type: "GET",
    jsonp: "finance_charts_json_callback",
    jsonpCallback: "finance_charts_json_callback",
    error: function( jqXHR, textStatus, errorThrown ) {
        console.log( jqXHR, textStatus, errorThrown );
    },
    success: function( data, textStatus, jqXHR ) {
        console.log( data );
    }
} );  

link looks like http://chartapi.finance.yahoo.com/instrument/1.0/a/chartdata;type=quote;ys=2012;yz=2;ts=1234567890/json?finance_charts_json_callback=finance_charts_json_callback

And it's response headers:

HTTP/1.1 200 OK
Date: Wed, 30 Apr 2014 12:01:08 GMT
P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO ... GOV"
Cache-Control: public
Expires: Thu, 01 May 2014 00:32:18 GMT
Last-Modified: Wed, 30 Apr 2014 00:32:18 GMT
Content-Type: text/javascript; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding,X-Ssl
Age: 0
Via: http/1.1 yts39.global.media.ir2.yahoo.com (...)
Server: ATS
Connection: keep-alive  

All works fine.

When I try to get file from another server there it's response headers:

HTTP/1.1 200 OK
Cache-Control: no-cache
Date: Wed, 30 Apr 2014 12:09:01 GMT
Pragma: no-cache
Content-Type: text/csv
Expires: -1
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
content-disposition: attachment;filename=export.csv
Content-Encoding: gzip
Vary: Accept-Encoding

解决方案

Edit, updated

Try

$(function() {
    $.getJSON("https://query.yahooapis.com/v1/public/yql?q=select"
              +"* from csv where url='http://finviz.com/export.ashx?'"        
              +"&format=json&diagnostics=true&callback=?"
      , function (data, textStatus, jqxhr) {
        $.each(data.query.results.row, function (index, value) {
            jqxhr.promise()
            .done(function () {
                    $("<li>", {
                      "text": value.col2 + " " + value.col8
                    })
                    .css("padding", "8px")
                    .appendTo("ol");
            })
            .always(function () {
                if (index === 4999) {
                  console.log(data.query
                            , data.query.diagnostics.warning
                            , data.query.results.row.length
                            , index
                            , $("li").length);
                };
            });
        });
    });
});

jsfiddle http://jsfiddle.net/guest271314/yxfEp/