0
我正在使用jquery.filedownload
插件與asp網頁API下載文件並顯示錯誤信息從服務器。jquery文件下載沒有回調工作
我已經安裝插件,並添加cookies來我的反應在GitHub上指示:https://github.com/johnculviner/jquery.fileDownload
我的文件被成功下載,但是回調不工作。
的js
var url = "/WebApi/PayrollBatches/GetBatchCsv?batchId=" + batchId;
$.fileDownload(url, {
successCallback: function (url) {
alert('success');
},
failCallback: function (responseHtml, url) {
alert('error');
}
});
return false;
//this is critical to stop the click event which will trigger a normal file download!
的Asp網頁API
[HttpGet]
public async Task<HttpResponseMessage> GetBatchCsv(int batchId)
{
string csv;
try {
using (var Dbcontext = new RossEntities()) {
PayrollBatchExport dal = new PayrollBatchExport(Dbcontext);
csv = await dal.GetBatchCsv(batchId);
}
}
catch (Exception ex) {
HttpError myCustomError = new HttpError(ex.Message) { { "CustomErrorCode", 42 } };
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError);
}
var cookie = new CookieHeaderValue("fileDownload", "true");
var cookiePath = new CookieHeaderValue("path", "/");
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(csv);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "Export.csv";
response.Headers.AddCookies(new CookieHeaderValue[] { cookie, cookiePath });
response.StatusCode = HttpStatusCode.OK;
return response;
}
編輯
下面是我的瀏覽器控制檯看起來在服務器錯誤響應像500:
將採取的外觀和恢復。謝謝 – JustLearning
成功回調正在工作。問題在於,即使服務器返回500,成功回調也總是受到攻擊。哪種破壞了我爲什麼使用此插件的全部要點。在您的測試場景中,您是否設法通過failCallback獲取錯誤消息? – JustLearning
@JustLearning我不是管理失敗的場景,但讓我檢查:) –