How to download file via jQuery ajax and C#

后端 未结 3 2065
悲哀的现实
悲哀的现实 2020-12-16 20:46

I want to download a file using jQuery Ajax web method, but it\'s not working.

Here is my jQuery ajax call to web method:

function GenerateExcel() {         


        
3条回答
  •  独厮守ぢ
    2020-12-16 21:42

    well i have done it using iframe

    this is the modified ajax function call

     function GenerateExcel() {
                var ResultTable = jQuery('
    ').append(jQuery('').append($('.hDivBox').find('thead').clone()).append($('.bDiv').find('tbody').clone())); var list = [$(ResultTable).html()]; var jsonText = JSON.stringify({ list: list }); $.ajax({ type: "POST", url: "GenerateMatrix.aspx/GenerateExcel", data: jsonText, contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { if (isNaN(response.d) == false) { $('#iframe').attr('src', 'GenerateMatrix.aspx?ExcelReportId=' + response.d); $('#iframe').load(); } else { alert(response.d); } }, failure: function (response) { alert(response.d); } }); }

    and this is the design part

     
    

    on Page load my code looks like this

     Response.AppendHeader("content-disposition", "attachment;filename=FileEName.xls");
     Response.Charset = "";
     Response.Cache.SetCacheability(HttpCacheability.NoCache);
     Response.ContentType = "application/vnd.ms-excel";
     Response.Write(tableHtml);
     Response.End();
    

    提交回复
    热议问题