【问题标题】:Fire javascript function after download asp.net下载asp.net后触发javascript函数
【发布时间】:2016-02-24 10:28:44
【问题描述】:

我在代码中有一个方法,可以将文件从服务器下载到客户端。之后我想运行一个javascript函数。但它不起作用。仅当 javascript 函数在按钮单击事件中时才有效。

string imageFilePath = Server.MapPath("~/TireLabel.png");
        Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file

        Graphics g = Graphics.FromImage(bitmap);
        string text = "215/45 R 20";
        g.DrawString(text, drawFontArial26Bold, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);

        text = "013";
        g.DrawString(text, drawFontArial20Regular, drawBrush, new RectangleF(70, 45, 0, 0), drawFormatRight);

        text = "1";
        g.DrawString(text, drawFontArial20Regular, drawBrush, new RectangleF(100F, 80, 0, 0), drawFormatRight);

        text = "2";
        g.DrawString(text, drawFontArial20Regular, drawBrush, new RectangleF(240, 80, 0, 0), drawFormatRight);

        Bitmap bit = new Bitmap(bitmap);
        bit.Save(Server.MapPath("~/TireLabel.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);

        Response.ContentType = "application/jpeg";
        Response.AddHeader("content-disposition", "attachment; filename=" + Server.MapPath("~/TireLabel") + ".bmp");
        Response.WriteFile(Server.MapPath("~/TireLabel.bmp" + ""));

        ClientScript.RegisterStartupScript(GetType(), "key", "runPrint();", true); // THIS does not fire.

//Javascript function
function runPrint() {
        var objShell = new ActiveXObject("WScript.Shell");
        var strCommand = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
        //var strCommand = "C:\\PrintLabel.exe";
        objShell.Exec(strCommand);
        //objShell.ShellExecute(strCommand, "","","open","1");
    }

如何让 javascript 触发。

【问题讨论】:

    标签: javascript c# asp.net .net


    【解决方案1】:

    这是一个难题,因为浏览器通常不会告诉您下载何时完成。看看jQuery.filedownload,它允许您通过 AJAX 下载文件。

    -- 编辑--

    这是将 jQuery 文件下载附加到所有带有“fileDownload”类的链接的代码:

     $(document).on('click', 'a.fileDownload', function() {        
    
       $.fileDownload($(this).prop('href'))
                .done(function () { alert('File download a success!'); })
                .fail(function () { alert('File download failed!'); });
    
            return false; //this is critical to stop the click event which will trigger a normal file download
    });
    

    这假定控制器操作的 URL 在链接的href 属性中给出,并且此操作返回带有return File() 的文件。 响应还必须包含一个 cookie /fileDownload 以通知 jquery.fileDownload 文件下载成功。

    //jquery.fileDownload uses this cookie to determine that a file download has completed successfully
    response.AppendCookie(new HttpCookie("fileDownload", "true") {
        Path = "/"
    });
    

    -- 编辑 2 - 更改源以显示更简单的示例,减少依赖。下载完成后使用.done 承诺触发操作

    【讨论】:

    • 虽然这可能是其核心的有效答案,但在其当前状态下它只是一个评论或仅链接答案。如果没有进一步的解释,它并不能解决 OPs 问题
    • 这是一个网络表单应用程序,也许我应该对你说。那么 href 属性中的 url 是什么?
    • 你能把你显示的C#代码转换成单独页面后面的代码,并使用这个页面的url来加载图像吗?很久没用没有MVC的纯asp.net了......
    • hm 我不明白你的意思,但如果我理解正确,我将创建一个新页面并从页面加载调用我的函数来下载文件?
    • 是的,在 MVC 中,这个函数会返回一个 FileResult ,不确定 asp 等价物是什么。也许Response.TransmitFile(file.FullName);,见stackoverflow.com/questions/8897458/…
    猜你喜欢
    • 1970-01-01
    • 2013-07-04
    • 2021-10-07
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多