我有 ajax 調用我的 web api 來獲取調用時生成的 xlsx 文件。xlsx 文件位于我知道是正確的內存流中,因為我直接從服務器下載了它并且沒有問題。但是,當我嘗試通過瀏覽器下載文件時,它說該文件無效。我正在使用 downloadjs 下載數據。我如何返回流 public HttpResponseMessage Function_Name() { var stream = getStream(); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; }我如何獲得流$.ajax({ url: "urlToData", headers: { 'Authorization': 'Authorization' }, success: function (data) { download(data, "test.xlsx"); } });當我運行此代碼時,我得到一個文件,但是該文件無法在 excel 中讀取。該文件也是 15.3 KB,其中直接從服務器下載的功能文件實際上是 10.0kb
1 回答

12345678_0001
TA貢獻1802條經驗 獲得超5個贊
我感到絕望并嘗試了這個:http : //www.henryalgus.com/reading-binary-files-using-jquery-ajax/
當我使用 XMLHttpRequest() 時它工作正常。
var xhr = new XMLHttpRequest()
xhr.open('GET', 'url', true);
xhr.setRequestHeader('Authorization', 'Authorization');
xhr.responseType = 'blob';
xhr.onload = function(e) {
if(this.status == 200) {
var blob = this.response;
download(this.response, "test.xlsx")
}
}
xhr.send();
- 1 回答
- 0 關注
- 237 瀏覽
添加回答
舉報
0/150
提交
取消