简体   繁体   中英

Fire javascript function after download asp.net

I have a method in code behind which downloads a file from server to the client. And after that I want to run a javascript function. But it does not work. It works only if the javascript function is in the button click event.

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");
    }

How I can make the javascript fire.

This is a hard problem, because the browser normally doesn't tell you when a download is finished. Have a look at jQuery.filedownload that allows you to download files through AJAX.

-- Edit --

Here is the code to attach jQuery filedownload to all links with the class "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
});

This assumes that the URL to the controller action is given in the href attribute of the link, and this action returns a file with return File() . The response must also contain a cookie /fileDownload to inform jquery.fileDownload that a successful file download has occured.

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

-- Edit 2 - Changed source to show simpler example with less dependendies. Use the .done promise to trigger an action after the download was done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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