使用filedownload jquery插件处理FilePathResult返回值

时间:2015-03-31 10:11:43

标签: javascript jquery asp.net-mvc

我正在使用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?
    }  

2 个答案:

答案 0 :(得分:1)

您可能会在errorMessage中抛出错误,并且jquery函数failCallback()应该捕获它。

else {
  throw new HttpException(404, errorMessage);
}

答案 1 :(得分:0)

this.Response.Clear();
            this.Response.StatusCode = 500;
            this.Response.ContentType = "text/html";
            this.Response.Write(errorMessage);
            throw new Exception(errorMessage);

上面的代码在javascript上调用了failCallback