ファイルに一連の文字を生成しますが、エラーがある場合、ファイルが不完全になる可能性があります。とにかくファイルを送信したいのですが、ユーザーに警告があります。だから私はこれを持っています:
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);