C# HttpGet response gives me an empty Excel file with EPPlus

前端 未结 1 367
暗喜
暗喜 2021-01-28 06:03

I\'ve created an endpoint that generates an Excel file. It\'s supposed to function as a GET in case I want some other code to POST it to a different endpoint for emailing, or in

1条回答
  •  深忆病人
    2021-01-28 06:57

    I use this to send the Excel file to the browser.

    HttpResponse Response = HttpContext.Current.Response;
    
    //first convert to byte array
    byte[] bin = excelPackage.GetAsByteArray();
    
    //clear the buffer stream
    Response.ClearHeaders();
    Response.Clear();
    Response.Buffer = true;
    
    //add the content type
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
    //set the content length, without it, length is set to -1 and could give errors
    Response.AddHeader("content-length", bin.Length.ToString());
    
    //add a filename
    Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".xlsx\"");
    
    //send the file to the browser
    Response.OutputStream.Write(bin, 0, bin.Length);
    
    //cleanup
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    

    0 讨论(0)
提交回复
热议问题