2016-12-02 46 views
1

我想解決我當前項目中的下列問題。Asp.net Response.end

現有

有一個aspx頁面其中有一個服務器端的click事件上的按鈕。我們根據所選的gridview行在此事件中進行一些處理,並根據所選記錄向瀏覽器顯示文件下載對話框。

功能改變

如果任何記錄,然後處理過程中失敗,我們要提示用戶,「有在加工過程中一些選定的記錄錯誤。是否要繼續使用的文件下載?」

方法我試圖解決這個問題

我觸發jQuery的功能從服務器端button_click事件顯示jQuery的對話框,告知用戶,有幾個錯誤處理期間選定的記錄。

ClientScript.RegisterStartupScript(GetType(), "Popup", "ShowPopup();", true);

我打電話使用Ajax的WebMethod它執行以下代碼:

response.Clear(); 
response.ContentType = "Application/Octet-Stream"; 
response.AddHeader("Content-Disposition", 
string.Format("Attachment; FileName={0}", Path.GetFileName(filePath))); 
response.WriteFile(filePath); 
response.End(); 

結果

我得到了彈出和Web方法也執行罰款,但我沒有看到瀏覽器中的任何文件下載對話框。我懷疑response.end在這種情況下不起作用。

回答

0

您無法通過AJAX下載文件。它必須通過單擊鏈接進行表單提交或GET請求。 Response.End工作正常:你做了而不是只是在ASP.NET中發現一個錯誤。

0

如果你想下載通過JavaScript你可以做這樣的事情由一個API端點返回的文件的文件,該代碼使用角呼叫,但它可以沒有它來完成,以及:

// Make an api call with the responseType of blob and do this with the result 
    const blob = new Blob([result], { type: result.type }); 

    if ($window.navigator.msSaveOrOpenBlob) { 
     $window.navigator.msSaveOrOpenBlob(blob, fileName); 
    } else { 
     const fileURL = URL.createObjectURL(blob); 
     const downloadLink = angular.element('<a></a>');// create a new <a> tag element 
     downloadLink.attr('href', fileURL); 
     downloadLink.attr('download', fileName); 
     downloadLink.attr('target', '_self'); 
     downloadLink[0].click();// call click function 
     URL.revokeObjectURL(fileURL);// revoke the object from URL 
    }