Download Excel file via AJAX MVC

后端 未结 14 1790
说谎
说谎 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:01

    I used the solution posted by CSL but I would recommend you dont store the file data in Session during the whole session. By using TempData the file data is automatically removed after the next request (which is the GET request for the file). You could also manage removal of the file data in Session in download action.

    Session could consume much memory/space depending on SessionState storage and how many files are exported during the session and if you have many users.

    I've updated the serer side code from CSL to use TempData instead.

    public ActionResult PostReportPartial(ReportVM model){
    
       // Validate the Model is correct and contains valid data
       // Generate your report output based on the model parameters
       // This can be an Excel, PDF, Word file - whatever you need.
    
       // As an example lets assume we've generated an EPPlus ExcelPackage
    
       ExcelPackage workbook = new ExcelPackage();
       // Do something to populate your workbook
    
       // Generate a new unique identifier against which the file can be stored
       string handle = Guid.NewGuid().ToString()
    
       using(MemoryStream memoryStream = new MemoryStream()){
            workbook.SaveAs(memoryStream);
            memoryStream.Position = 0;
            TempData[handle] = memoryStream.ToArray();
       }      
    
       // Note we are returning a filename as well as the handle
       return new JsonResult() { 
             Data = new { FileGuid = handle, FileName = "TestReportOutput.xlsx" }
       };
    
    }
    
    [HttpGet]
    public virtual ActionResult Download(string fileGuid, string fileName)
    {   
       if(TempData[fileGuid] != null){
            byte[] data = TempData[fileGuid] as byte[];
            return File(data, "application/vnd.ms-excel", fileName);
       }   
       else{
            // Problem - Log the error, generate a blank file,
            //           redirect to another controller action - whatever fits with your application
            return new EmptyResult();
       }
    }
    

提交回复
热议问题