0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

14 Comments

While this may be a valid answer in its core, it is only a comment or a link-only-answer in its current state. It doesn't solve the OPs problem without further explanation
it is a webforms app maybe I should say to you. So what the url in href attribute ?
Can you warp the C# code you have shown into the code behind of a separate page and use the url of this page to load the image? It has been a long time since I used pure asp.net without MVC....
hm I dont know exactly what you mean, but if I understand right I going to create a new page and call my function from page load to download the file?
Yes, in MVC this function would return a FileResult , not sure what the asp equivalent is. Maybe Response.TransmitFile(file.FullName);, see stackoverflow.com/questions/8897458/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.