简体   繁体   English

同时执行Javascript和控制器ActionResult

[英]Execute Javascript and controller ActionResult simultaneously

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? 这个问题开始,我先前想知道的是-在控制器的操作返回了File并将其写入响应后,是否还可以显示Javscript警报?

Please note that this project is MVC 1.0 请注意,该项目是MVC 1.0

This is the ActionReult where I want to execute some Javascipt, just before the file downloads: 在文件下载之前,这是我要执行一些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 Method for processing the queue inside the usercontrol: 用于处理用户控件内部队列的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;
}

Basically, the file downloads but the Javascript alert does not show. 基本上,文件已下载,但不显示Javascript警报。 The UserMessageUtil.Info() is used in other parts of the project and works as expected, showing alerts when executed. UserMessageUtil.Info()用于项目的其他部分,并按预期工作,在执行时显示警报。

If I understand correctly, you want some client side javascript to execute once the file is ready to download ? 如果我理解正确,一旦文件准备好下载,您希望一些客户端javascript执行吗?

I've had similar needs although it was with Java EE the technical problem remains the same. 尽管Java EE的技术问题仍然相同,但我也有类似的需求。 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 看看这辉煌的黑客在这里http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx为特色在这个答案检测时浏览器收到文件下载

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. 基本上,您想从您的操作中设置一个Cookie,该Cookie将与服务器响应一起返回,并在客户端进行一些JavaScript轮询以了解该Cookie的存在。 Once the server responds and the file download dialog is shown to the client, the cookie is detected and your script executed. 服务器响应并向客户端显示文件下载对话框后,将检测到cookie并执行您的脚本。 Once the script is executed, you can destroy the cookie. 执行脚本后,您可以销毁cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM