[英]How to show a message and send a file using ASP.NET
我在文件中生成了一組字母,但是如果有錯誤,則文件可能不完整。 無論如何,我都想發送文件,但要向用戶發出警告。 所以我有這個:
string errMessage;
string filePath = CCGTMarblox.Admin.Helpers.DocumentHelper.GenerateRejectionLetters(grantIDs, out errMessage);
if (!String.IsNullOrEmpty(errMessage))
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "problem", String.Format("alert('{0}');", errMessage), true);
}
if (filePath != null)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", string.Format(@"attachment; filename=""{0}""", fileInfo.Name));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.TransmitFile(fileInfo.FullName);
Response.Flush();
}
問題在於,當包含下載文件的代碼時,警報不會顯示-標題被清除,就像新的響應一樣。 有什么辦法可以發送腳本和文件? 我試圖將Response.Redirect重定向到一個檢索文件的頁面,但這也不起作用。
實際上,我最后做的是發送另一個啟動腳本,該腳本執行與我在問題中嘗試的重定向相似的任務。 (我已經有一個處理下載的頁面)。
編輯(接受答案后添加)
string script = String.Format("window.location = '{0}';", url);
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "goToDownload", script, true);
我建議有一個頁面,該頁面指示用戶文件下載將很快開始,並且在此頁面中,您可以顯示任何警告。
該頁面將使用JS /元刷新來單獨請求文件,然后繼續下載。 如果下載未在指定的時間開始,您可能還想提供直接鏈接。
您無法在單個GET中返回HTML和另一個響應(針對文件流)。
您可以返回HTML並具有下載文件的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.