Following on from this question I previously asked I would like to know - After my controller's action has returned the File, and has been written to the response, is it possible to show a Javscript alert also?
Please note that this project is MVC 1.0
This is the ActionReult where I want to execute some Javascipt, just before the file downloads:
[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 Method for processing the queue inside the 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;
}
Basically, the file downloads but the Javascript alert does not show. The UserMessageUtil.Info()
is used in other parts of the project and works as expected, showing alerts when executed.