2

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.

Community
  • 1
  • 1
Seany84
  • 5,526
  • 5
  • 42
  • 67
  • I'm just wondering if I should abandon this approach and maybe redirect the user to a page/view with the message and a link to download the generated file. This breaks my existing UI/UX but it's better to have something rather than nothing at this point. – Seany84 Feb 07 '13 at 20:35

1 Answers1

0

If I understand correctly, you want some client side javascript to execute once the file is ready to download ?

I've had similar needs although it was with Java EE the technical problem remains the same. Take a look at this brilliant hack here http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx as featured in this answer Detect when browser receives file download

Basically, you want to set a cookie from your action which will be returned with the server response, and have some javascript poll for the presence of that cookie on the client side. Once the server responds and the file download dialog is shown to the client, the cookie is detected and your script executed. Once the script is executed, you can destroy the cookie.

Community
  • 1
  • 1
Drewman
  • 947
  • 11
  • 23