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.

Sooraj
  • 9,717
  • 9
  • 64
  • 99

1 Answers1

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

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • 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 – Marco Feb 24 '16 at 10:36
  • it is a webforms app maybe I should say to you. So what the url in href attribute ? – AndreasPettersson Feb 24 '16 at 12:25
  • 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.... – Georg Patscheider Feb 24 '16 at 13:19
  • 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? – AndreasPettersson Feb 24 '16 at 14:10
  • Yes, in MVC this function would return a `FileResult `, not sure what the asp equivalent is. Maybe `Response.TransmitFile(file.FullName);`, see http://stackoverflow.com/questions/8897458/asp-net-download-file-to-client-browser – Georg Patscheider Feb 24 '16 at 15:49
  • I get this error : Property or method filedownload is not supported by the object. So i guess I came in to my function of my button. @GeorgPatscheider – AndreasPettersson Feb 25 '16 at 07:16
  • what should i type in href on the tag ? and what should I type in the $(this).attr('What should i type in here?') @GeorgPatscheider – AndreasPettersson Feb 25 '16 at 07:45
  • You have to include the jQuery.fileDownload.js script on your page. The `href` attribute of the link should point to the page that delivers the file to be downloaded. This URL is then read by `$(this).attr('href')`. – Georg Patscheider Feb 25 '16 at 11:16
  • in my default.aspx page I have Push here!. And have this script: And on my Print page i have a method called AjaxMethodTest(), so i should call that method from the page load on print page? I now get an error: Property or method dialog is not supported by the object – AndreasPettersson Feb 25 '16 at 12:34
  • dialog is part of jquery-ui – Georg Patscheider Feb 25 '16 at 12:44
  • Okay, now I have jquery-ui also . but when press my button now with href to my print page, and from print page, pageload i calling my method that saves file to client it just saves the file and does not go thru the jquery.filedownload javascript...... – AndreasPettersson Feb 25 '16 at 13:15
  • Please try again with the `.done` promise (see my updated answer). – Georg Patscheider Feb 25 '16 at 19:34
  • I tried with the .done promise but I did not get it to work either. Here I have paste in my code now so if u have time you can check it maybe see what's wrong. http://pastebin.com/2rA7bT04 @GeorgPatscheider – AndreasPettersson Feb 26 '16 at 08:23
  • I tried the demo: http://jqueryfiledownload.apphb.com/ And I try to download the report the alert message gets to me before I have downloaded the file. That felt very weird? And I need to have it saved before i run the .done : `$(document).on("click", "a.fileDownloadPromise", 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 });` – AndreasPettersson Feb 26 '16 at 09:46