Download Excel file via AJAX MVC

后端 未结 14 2012
说谎
说谎 2020-11-22 02:30

I have a large(ish) form in MVC.

I need to be able to generate an excel file containing data from a subset of that form.

The tricky bit is that this shouldn

14条回答
  •  感动是毒
    2020-11-22 03:07

    I am using Asp.Net WebForm and just I wanna to download a file from server side. There is a lot article but I cannot find just basic answer. Now, I tried a basic way and got it.

    That's my problem.

    I have to create a lot of input button dynamically on runtime. And I want to add each button to download button with giving an unique fileNumber.

    I create each button like this:

    fragment += "
    ";

    Each button call this ajax method.

    $.ajax({
        type: 'POST',
        url: 'index.aspx/CreateExcelFile',
        data: jsonData,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (returnValue) {
          window.location = '/Reports/Downloads/' + returnValue.d;
        }
    });

    Then I wrote a basic simple method.

    [WebMethod]
    public static string CreateExcelFile2(string fileNumber)
    {
        string filePath = string.Format(@"Form_{0}.xlsx", fileNumber);
        return filePath;
    }
    

    I am generating this Form_1, Form_2, Form_3.... And I am going to delete this old files with another program. But if there is a way to just sending byte array to download file like using Response. I wanna to use it.

    I hope this will be usefull for anyone.

提交回复
热议问题