使用filedownload jquery插件处理FilePathResult返回值
Handling FilePathResult return values using filedownload jquery plugin
我正在使用fileDownload jQuery插件下载一个使用ASP.NET MVC提供服务的文件。该操作使用HttpGet进行修饰,并返回一个FilePathResult。它在找到文件时工作。如果找不到文件,我不确定在操作中返回什么?
JavaScript:
function showMessageCancelDialog(message, request, titleValue) {
'use strict';
$('#messageDialog').html(message);
$('#messageDialog').dialog({
resizable: false,
width: '320px',
modal: true,
title: titleValue,
dialogClass: 'no-close',
buttons: {
Cancel: function () {
request.abort();
$(this).dialog("close");
}
}
});
}
function hideMessageDialog() {
'use strict';
$('#messageDialog').dialog("close");
}
function DownloadAttachment(itemId) {
'use strict';
var getAttachmentFileUrl = "/App/Download/GetAttachmentFile?ItemId=" + itemId;
var ajaxRequest = $.fileDownload(getAttachmentFileUrl, {
successCallback: function (message) {
hideMessageDialog();
},
failCallback: function (errorMessage) {
hideMessageDialog();
showMessageDialog("Download request failed:" + errorMessage, "Download attachment");
}
});
showMessageCancelDialog("Download in progress... , ajaxRequest, "Download attachment");
}
ASP.NET:
if(errorMessage == string.Empty)
{
this.Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFile);
return new FilePathResult(downloadFile, "application/octet-stream");
}
else
{
// WHAT DO I RETURN HERE?
}
您可能会在errorMessage
中抛出一个错误,jquery函数failCallback()应该会捕捉到这个错误。
else {
throw new HttpException(404, errorMessage);
}
this.Response.Clear();
this.Response.StatusCode = 500;
this.Response.ContentType = "text/html";
this.Response.Write(errorMessage);
throw new Exception(errorMessage);
上面的代码在javascript 上调用了failCallback