首先定義方法的返回值需要為HttpResponseMessage
定義返回的對象
var response = new HttpResponseMessage();
定義一個流接收
MemoryStream StreamFile=//加載的數據
接下來是處理
response.Content = new StreamContent(StreamFile); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = "導出的Excel名稱.xls"; return response;
接下來是JavaScript代碼
window.location=APIURL+"/api/report/excels";
調用之后,瀏覽器便會把這個Excel下載了