继这个问题之后,我之前问过我想知道 - 在我的控制器的操作返回文件并写入响应后,是否也可以显示 JavaScript 警报?
请注意,这个项目是MVC 1.0
这是我想在文件下载之前执行一些 Javascipt 的 ActionReult:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SomeImporter(HttpPostedFileBase attachment, FormCollection formCollection, string submitButton, string fileName)
{
if (submitButton.Equals("Import"))
{
byte[] fileBytes = ImportClaimData(fileName, new CompanyExcelColumnData());
if (fileBytes != null)
{
//This method stores the string in a message queue which is stored in the
//session and rendered from within a UserControl. The Javascript alert
//is then shown subsequently.
UserMessageUtil.Info("Data imported successfully.", Session);
//The file downloads but no Javascript alert is shown from the line above.
return File(
fileBytes,
"application/ms-excel",
string.Format("Import {0}.xls", DateTime.Now.ToString("yyyyMMdd HHmm")));
}
return View();
}
throw new ArgumentException("Value not valid", "submitButton");
}
用于处理用户控件内部队列的 HTML Helper 方法:
public static string UserMessage(this HtmlHelper helper, HttpSessionStateBase session)
{
MessageQueue queue = UserMessageUtil.GetMessageQueue(session);
Message message = queue.Next;
String script = null;
if (message != null)
{
do
{
string messageStr = message.Text;
....
script += @"alert('" + messageStr + "');";
}
while ((message = queue.Next) != null);
return script;
}
return null;
}
基本上,文件会下载,但不会显示 Javascript 警报。用于项目的UserMessageUtil.Info()
其他部分并按预期工作,在执行时显示警报。