2015-03-31 67 views
0

我正在使用fileDownload jQuery插件来下载使用ASP.NET MVC提供的文件。该操作使用HttpGet进行装饰并返回FilePathResult。它在找到文件时起作用。如果找不到文件,我不确定该操作返回什么内容?使用文件处理FilePathResult返回值jQuery插件

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

回答

1

你可以抛出一个错误,你errorMessage和jQuery函数failCallback()应该赶上。

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

我试过,但 “showMessageCancelDialog” 之称的对话框仍然存在的failCallback。只有当我点击“取消”,它才会进入failCallback。 – 2015-03-31 10:26:11

+0

这是因为在接收到来自服务器的ajax响应之前* showMessageCancelDialog *被执行。另外,* showMessageCancelDialog *中有一个输入错误,在''下载进程中缺少'“字符...' – Niklas 2015-03-31 15:18:30

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

上面的代码中调用JavaScript的