从this question I previously asked继续我想知道 - 在我的控制器操作返回文件后,并且已写入响应,是否也可以显示Javscript警报?
请注意,此项目为MVC 1.0
这是ActionReult,我想在文件下载之前执行一些Javascipt:
[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用于处理usercontrol内部队列的方法:
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()
用于项目的其他部分,并按预期工作,在执行时显示警报。
答案 0 :(得分:0)
如果我理解正确,您希望在文件准备下载后执行一些客户端javascript?
虽然Java EE的技术问题仍然存在,但我有类似的需求。在这个答案http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
中看到这个精彩的黑客Detect when browser receives file download基本上,您希望从您的操作中设置一个cookie,该cookie将随服务器响应一起返回,并对客户端存在该cookie进行一些javascript轮询。一旦服务器响应并向客户端显示文件下载对话框,就会检测到cookie并执行脚本。执行脚本后,您可以销毁cookie。