2015-03-31 18 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的