Ranjith Venkatesh
Ranjith Venkatesh

Reputation: 1362

Handling FilePathResult return values using filedownload jquery plugin

I am using fileDownload jQuery plugin to download a file which is served using ASP.NET MVC. The action is decorated with HttpGet and returns a FilePathResult. It works when a file is found. If a file is not found, I am not sure what to return in the action?

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?
    }  

Upvotes: 0

Views: 630

Answers (2)

Ranjith Venkatesh
Ranjith Venkatesh

Reputation: 1362

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

The above code called the failCallback on javascript

Upvotes: 0

Niklas
Niklas

Reputation: 13155

You could throw an error with your errorMessage and the jquery function failCallback() should catch that.

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

Upvotes: 1

Related Questions