Geeks With Blogs

News Meta

Gruff Code Code and tech musings from Jesse Taber. If I call it a blog I'll feel bad when I don't update it every week.

This post has moved to a new host:

http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/

Posted on Thursday, October 28, 2010 4:38 PM | Back to top


Comments on this post: Detecting the File Download Dialog In the Browser

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This solution is perfect! Works great with Spring MVC and a JSP. Thank you!
Left by Brian on Nov 03, 2010 9:34 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Nice, thank you very much, but doesn't work on ie6 for me.. IE8, FF, opera, chrome are good, but old ie6 not :-(
Left by pietro on Nov 16, 2010 9:00 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@pietro: What is the issue you're seeing in IE 6? I've tested this out in IE 6 before and have had no problems with it. Drop me a line on the contact form of the blog and maybe I can help you.
http://geekswithblogs.net/GruffCode/contact.aspx
Left by Jesse on Nov 17, 2010 10:58 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
All is OK now.
I applied your demo program to my java program. So I set header, cookie...
You have response.Flush();
I was sending the file using OutputStream, so I didn't do this step. But it wasn't function in IE6.
So I tried response.flushBuffer(); in java and finally it works.
Thanks for great demo program Jesse ;-)
Left by pietro on Nov 18, 2010 7:21 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Hi Pietro,

Glad that you are able to do it for java, can you pl share that code for java. Thanks

yuva
Left by Yuva on Dec 28, 2010 12:07 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Can this be achieved without the use of JQuery?
Left by Lakshmi Narayanan on Dec 28, 2010 4:51 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@Lakshmi: This same technique can be accomplished without jQuery, though you would end up having to write a lot more client-side code to make it work (unless you were using some other javascript library). I know and like jQuery, so that's what I used.
Left by Jesse on Jan 02, 2011 8:44 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This was a huge help! Thank you so much!
Left by DevFox on Jan 26, 2011 2:52 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thank you for this very elegant solution!

However you reference a question from eFreedom which is wrong: this site only plagiarize stackoverflow.com and messes all search engines results. As indicated on eFreedom, this question is copied from
http://stackoverflow.com/questions/3055138/can-you-use-javascript-to-detect-a-file-download-window-created-server-side

p.s. : the comment form does not accept my email address since it has a “+” in it :(
Left by Didier on Jan 28, 2011 10:55 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@Didier: Thanks for pointing me to the correct question on Stack Overflow. I've updated the post to reflect that. When I first published this I didn't know what eFreedom was, and didn't know any better. I'm glad to give proper credit to the site that actually contained the original question.
Left by Jesse on Jan 28, 2011 11:03 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
You rock! One other cool thing is that the cookie is available in ALL browser windows referencing the same domain/path, so you can monitor the presence of the cookie in the "main" window, but load the d/l into a separate window (which could be nice depending on your needs).
Left by dmans on Feb 03, 2011 10:11 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This is a very nice solution. I was searching for this for some time until I found it. BTW, I am also using jQuery and blockUI on the client side, the server side is spring MVC. Thanks for sharing this with everybody.
Left by Salman on Feb 21, 2011 12:16 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This is beautiful, thanks!
Left by Gary on Mar 10, 2011 1:12 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
You beauty! Simple and elegant solution. Thanks so much.
Left by Mo on Mar 21, 2011 12:16 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
very nice,hope this can help me.thanks for your great work,man.

can i add you on facebook:

my is : ZhongHui Duan
Left by zhonghui on Apr 19, 2011 5:11 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This is great code - thank you!

For the php users out there, the php equivalent of

AppendCookie(new HttpCookie("fileDownloadToken", downloadTokenValue);

in php is:

setcookie ("fileDownloadToken", $downloadTokenValue);

CJA
Left by cja on Apr 27, 2011 4:02 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
What a great idea. This worked out perfectly for what I wanted to do. Thanks for sharing this technique.
Left by TAmes on Apr 27, 2011 11:43 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks... this was a great post... i have been struggling for this sort of solution. Thanks a lot for sharing.

for those who are facing issue in reading cookie...

Set cookie.HttpOnly = false

Left by Adil on May 06, 2011 9:42 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Hi, I just come across your post, it's brilliant.
Until now I've been using the window.onblur event to detect when the download dialog appears and hide the "please wait" div. I works fine provided the user don't decide to read an email during the wait.
I will definitely try your solution, thanks!
Left by paolosca on Jun 04, 2011 1:06 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Perfect solution. Adapted it to Java w/out any issues.
Left by Yuriy Shikhanovich on Jul 14, 2011 4:37 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Works Perfect. Thanks a lot
Left by Phani Kumar on Jul 27, 2011 12:30 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
I have a ajaxForm , when the user submits the page it hits the sever url and necessary page gets generated. But with the example that you have shown here how do I open a pop up window for the incoming file to download.

Can you please provide an example how to achieve it.

My way of sending and opening popup is $('#reportForm').ajaxForm( {

dataType :'json',
type :'POST',
url : 'report/initialRequest.html',
beforeSubmit :validateSearchField,
ifModified : true,
success :function(data, textStatus, jqXHR){},
complete : function(jqXHR, textStatus) {
window.location.href = "report/initialRequest.html" + "?" + $('#reportForm').formSerialize();
$.unblockUI();
return false;
}
});

But in this case what happens its send in another request again and reprocess what it has done earlier.

Can you please help me on this?
Left by srinivas on Jul 30, 2011 4:23 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
What a brilliant hack. :) The only browser I was able to get working without this trick was IE9 using document.readyState. Doesn't work so much with Chrome/FireFox.
Left by Blake on Aug 10, 2011 11:53 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Nice one. I was looking for exactly this. Thanks very much.
Left by Pete on Aug 12, 2011 7:03 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
That's awesome! Exactly what I was looking for.
Thank you very much for sharing this.

Left by Mohammad on Aug 18, 2011 10:09 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Hi, I'd like to have the Java-JSP translation if possible. I tried to translate but it's not working for me. Thank you.
Left by Marta Ramirez on Aug 18, 2011 12:32 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
You are my hero. I have been struggling with this issue for months! Thanks!
Left by BethW on Sep 01, 2011 8:42 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Brilliant! This is exactly what I needed. Thanks a bunch!
Left by Vlad on Sep 02, 2011 6:13 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
There seems to be a problem On safari for this solution. No cookie is written.
Left by Wu Huajie on Sep 05, 2011 6:22 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
I'm in total debt to you. I've been trying to get a simple, minimal method of doing this forever! thanks!
Left by Paul J on Sep 15, 2011 2:12 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
i have a similar problem, in my case , i have a small zip file say of 400-500 bytes generated on air and passed on to browser for download which opens up download popup, by this time backend already had updated with file consumption from client, now delimma is when users clicks cancel,i wish to let backend rollback my changes, how can this be achieved. i am sick of this issue kindly help.
Left by strive on Sep 17, 2011 2:08 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
You saved my life... :-)
Left by Srinivas on Sep 24, 2011 11:01 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This solution works me fine in Chrome and FF, but I have a problem with IE9 (IE in general). When the response with the file comes it contains a set-cookie header with the proper value, but when I read the cookie with the jquery function it doesn't read the proper value (it reads the value of the cookie I set when I load the page to test that I'm readingg the cookie properly). It seems like the browser is not setting the cookie due to is a file download response, if i reload the page the cookie of the response is not set.

The server language is PHP, but the solution works fine in FF and Chrome.

Any idea?? Thanks in advance.
Left by Enrique Andreu on Sep 29, 2011 9:07 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
How to set cookie in asp application and how to read it in jquery?
I have set the cookie in asp like this Response.AddHeader "Set-Cookie", "fileDownloadToken=something;path = /;"

But Jquery is not reading this.

I am using IE 6.0.Please help me.
Left by Ram on Oct 07, 2011 3:31 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
It works perfectly! Thanks for sharing this great gem!

Cheers,
Lee
Left by Shih-gian Lee on Oct 17, 2011 3:09 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
How genius idea is!
Exactly what I was looking for!
Thank you so much. :D
Left by herme on Oct 19, 2011 12:50 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
the cookies was written to the client must be after file sending completely?
Left by phil on Oct 20, 2011 12:24 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@phil - I don't think I understand your question. The cookies should be sent to the browser in the same HTTP Response that contains the "data" for the file to be downloaded. In this way the 'block UI' stuff should go away as soon as the file download dialog appears.
Left by Jesse on Oct 20, 2011 4:46 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
if the file not send completely(might be paused by the user).will the cookies be sent to the browser?
Left by phil on Oct 22, 2011 12:50 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
sorry I misunderstood this post, I thought this solution is used to detect whether file was already download.. but many thanks you share the very good idea.
Left by Phil on Oct 22, 2011 8:53 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
A great post. You said the sample application is adapted from web form version. Can you post or email me the web form version. I have a project still in ASP.NET 2.0. A lot of efforts is required to migrate to MVC. Thanks!
Left by willie on Oct 30, 2011 5:15 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
man, you saved my life! i spent two freakin' days on this, and your solution works perfectly. thank you!
Left by fattynoparents on Nov 17, 2011 1:23 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks a lot!
Left by Yuri on Nov 17, 2011 8:22 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Works great except in IE for PDF files. I was able to open the PDF files before applying this solution, however, after changing the code to the above solution, I am getting 'There was an error opening this document. This file cannot be found.'

Error only occurs in IE for PDF files. Does anyone have a clue?
Left by Steve on Nov 24, 2011 12:52 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
You save my life! Thanks!!
Left by Gabriele Gaggi on Nov 24, 2011 9:30 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
One thing for sure..

YOU SAVED MY LIFE!!

a week of headache gone instantly once i found and adapt your concept to my code..

THANK YOU..

TERIMA KASIH
Left by Denny Saviant on Nov 30, 2011 12:47 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Wow!! good solution ...
But..... this will work only if cookie is enabled. Right?
what if it is an environment where cookies are not enabled?
will the data be passed as url encoded?
Left by stv on Dec 05, 2011 1:06 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@stv

This solution will only work for browsers with cookies enabled.
Left by Jesse on Dec 05, 2011 8:58 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
You save my life hero
Thank you a lot
Great solution!
Left by Duong on Dec 08, 2011 2:12 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
I have been seeking this code for a long time! Great Solution!
Thank you very much

This idea is good for many things!
Left by rspaz16 on Dec 22, 2011 12:14 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
What a great solution. Thanks.
Left by Tom on Jan 14, 2012 1:37 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This is a brilliant solution, thanks!

One issue with it though in the example solution is the timer keep's running/doing the comparison. So you keep calling the finishDownload() over and over which might not be desirable (in my case, on subsequent 'downloads' my submit button which I was disabling until the file was generated kept getting enabled straight away).

Easily solved by clearing the timer (I used http://stackoverflow.com/a/2133217).

:)
Left by Randeep Singh on Feb 22, 2012 9:45 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
this solutions rocks. i use it with php. great!
Left by Micha von Vau on Mar 08, 2012 11:10 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks! This is exactly what I was trying to do, but then didn't differently and now changed it to this better version.
Left by Chris on Mar 09, 2012 2:08 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Excellent ! Still useful these days !
Left by none on Apr 18, 2012 4:35 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This solution is perfect! Works great with JSP. Thanks a lot!
Left by Kevin on Apr 28, 2012 5:43 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Great article. works perfectly in asp.mvc 3
Left by suresh on May 09, 2012 2:19 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Sorry, but it's doesn't work for me. I need to detect when a file finish download inside a IFRAME.
Always when I read the cookie with jquery, its value is NULL
This is my java code:

response.addCookie(new Cookie("tokenDocumentLoad", token));
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control","no-store");

response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
response.addHeader("Pragma", "public");
response.addHeader("Cache-Control", "max-age=0");
response.setDateHeader("Expires", (System.currentTimeMillis() + new Integer(getServletContext().getInitParameter("webAppResponseExpire")).intValue()));
ServletOutputStream servletOutputStream = response.getOutputStream();

fileContent....

servletOutputStream.flush();
servletOutputStream.close();
-------------------
This is the javascript code:

var cookieValue = $.cookie('tokenDocumentLoad');
alert("cookieValue:" + cookieValue);

Left by dobrobuz on May 18, 2012 12:00 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Hi! Great post!
I had to add the 'name' attribute to the hidden field in order for this to work. Otherwise, the browser didn't send the parameter (maybe it had to do with the form issuing a GET request)
<input type="hidden" id="download_token_value_id" name="download_token_value_id"/>

Regards!
Left by Rafael Sisto on May 31, 2012 3:16 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Your article helped me a lot. I appreciate your efforts.
Thank you very much.
Left by Ahmet on Jun 19, 2012 8:13 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
thank you so much for this post - saved me a lot of time! This is the only solution that worked first time, every time! Used with PHP Yii.

may you live long and prosper!!
Left by zu on Jun 26, 2012 2:01 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks Jesse!
Left by From BRazil on Aug 21, 2012 8:34 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
what if user has disabled the cookie.
Will this work
Left by jobs on Sep 08, 2012 10:34 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@jobs: This approach will only work if the user's browser accepts cookies.
Left by Jesse on Sep 10, 2012 9:33 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
great post.

one little hitch that took a while to fix: I needed to set the path variable on the cookie to '/'. without this, the jQuery doesn't find the cookie because it is referenced to the path of the download link, rather than the path of the current page.

dunno if this makes sense or not... i found this only after a few hours of trial and error and I'm still learning some of the web protocol stuff by trial and (lots of) error.
Left by Mark Blackwell on Sep 16, 2012 10:10 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Very elegant solution I love it. Thanks for the MVC3 example on Github, I do everything in MVC so it was easy to implement.

@Mark Blackwell and others
I ran into that as well. The thing to keep in mind is it might not always be / (root) dependent on the language you use to create the cookie. For .net HttpCookie defaults to root so you just need to send the null value to root
$.cookie('ExcelDownloadToken', null, { path: '/' });

It sounds like you got it working but figured it worth a mention for others since this is obviously the best solution for cookie enabled browser sessions.
Left by Richard Kuhn on Nov 27, 2012 1:37 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks so much for this post. It is genius and solves my exact issue.
Left by Marc W on Dec 12, 2012 3:16 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
This works great except for one thing. If the modal is displayed and the user hits the stop button or presses escape, this effectively cancels the download, and the modal never goes away. How can I get around that?
Left by Rusty D on Dec 16, 2012 10:16 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@Rusty D: That's a good point and a weakness of this approach. I'm not sure if there's a way that you could detect the fact that the user hit the stop button, though you might be able to detect whether or not they have hit 'esc'. If I have time I'll look into that and see if there might a workaround.
Left by Jesse on Dec 17, 2012 9:02 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Hm... this looks not working with IE9. Anybody found a solution for it?
Left by kevin on Jan 11, 2013 5:25 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks mate. I cant say how immensely your post helped me.
Left by Ravi on Jan 16, 2013 5:23 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Please help me PHP guys. I setup a cookie in PHP and then try to read from client(jquery) but it just said something like "token = object object". Client read from with jqeury and setCookie in PHP... is this really working?
Left by kevin on Jan 18, 2013 1:02 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks for this.But in IE8 it's not working perfectly.when the download popup comes , the loading state is not getting hidden.it is getting hidden only when i cancel or save the download.it works perfectly for all other browsers. plz help me here
Left by naveen on Feb 13, 2013 1:45 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Your a genius! Thanks so much for sharing this
Left by Michael on Feb 20, 2013 11:02 PM

# Thanks!
Requesting Gravatar...
Just echoing what many have already said - great solution, many thanks.
Left by Chris Valdivia on Apr 11, 2013 11:19 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks a lot! It's a really nice solution and it's exactly what I needed :)
Left by Bartosz Zawistowski on May 27, 2013 5:51 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks a lot! Good solution.. Just one comment. Running my project with debug, that row $.cookie('fileDownloadToken', null); works great, but when project is published, that row isnt working as expected.. I replaced this previous row with $.removeCookie('fileDownloadToken'), and now all works great... Thanks again.
Left by Valters on Jun 26, 2013 4:47 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks for this! I had the same problem as Valters. When you click the create file button a second time it wasn't working. Replacing the line $.cookie('fileDownloadToken', null); with $.removeCookie('fileDownloadToken') works fine for me now, no matter how many times I click create download! Thanks Valters!
Left by Daren on Jul 01, 2013 4:53 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
@Daren and @Valters: Thanks for pointing that out. I guess the syntax of that plug-in has changed since I wrote this post initially. I've updated the post accordingly.
Left by Jesse on Jul 01, 2013 9:34 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thank you very much for this! It's an elegant,classy solution. Just what we were looking for. I have it working with a ColdFusion site and it was easy to implement.
Left by Big Barn Owl on Oct 11, 2013 3:58 PM

# Thanks
Requesting Gravatar...
I just wanted to say thank you for this example - it helped me greatly. Thanks!
Left by Ryan J on Nov 06, 2013 11:06 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Bull's eye! I have been looking for a solution for exactly the same problem and my colleague stumbled upon this blog! Thank you!
Left by Shyam on Nov 20, 2013 8:02 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thank you so much. It saved my day.
Left by Suba on Dec 06, 2013 3:08 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks, this is really useful. Works on WinForms 3.5 also.
Left by Basil on Jan 27, 2014 8:58 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
I'm struggling to get this working in my implementation and wonder if anyone can see why.
The 'Please Wait' is displayed but the cookie is never matched and so the screen remains showing 'Please Wait' forever. I've tried returning a response from the Server side instead of 'void' but this never works either.

I've simplified the code as there's a lot of detail in the original but basically I generate an Excel file which contains an image and some data then download this (all of which works). All I'm missing is being able to clear the 'Please Wait'.

The user clicks on btnGraphExportToExcel to start things off.....

-----------
CLIENT SIDE
-----------

<div>
<br />

<span>Download What If</span>

</div>


function ExportGraph(){
var url = '@Url.Action("GraphExportToExcel", "ExcelHelper", new RouteValueDictionary(new { fileURL = "PARAM1", mstartDate = "PARAM2", mendDate = "PARAM3", period = "PARAM4" }))';
// These values are all defined elsewhere
url = url.replace("PARAM1", imageName);
url = url.replace("PARAM2", startDate);
url = url.replace("PARAM3", endDate);
url = url.replace("PARAM4", _display_period);
url = url.replace(/&amp;/g, "&");
myLink = url;

loadMyLink(imageName);
}

function loadMyLink(imageName) {
$.blockUI();
window.location.href = myLink;

fileDownloadCheckTimer = window.setInterval(function () {
var cookieValue = $.cookie('fileDownloadToken');
if (cookieValue == imageName) {
finishDownload();
}
}, 1000);
}

function finishDownload() {
window.clearInterval(fileDownloadCheckTimer);
$.cookie('fileDownloadToken', null);
$.unblockUI();
}

------------------------------------------------------------
SERVER SIDE (ExcelHelperController.cs -> GraphExportToExcel)
------------------------------------------------------------

public void GraphExportToExcel(string fileURL, string mstartDate, string mendDate, string period)
{
// Create a spreadsheet and populate it with the required data
// ALL THIS WORKS AS EXPECTED

// Provide the download dialog for the file
string folderPath = ConfigurationManager.AppSettings["ExcelFolderPath"] + "/" + Guid.NewGuid();
string serverExcelFilePath = Request.MapPath("~" + folderPath + "/");

string dateStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
string excelPath = serverExcelFilePath + "ExcelHelper_" + dateStamp + ".xlsx";
MakeFileDownloadable(excelPath, fileURL);

// Delete the image from the server
DeleteImageFileFromServer(fileURL);

// Delete the Excel file from the server - THIS MUST BE DONE AFTER THE FILE HAS BEEN DOWNLOADED
DeleteExcelStore(folderPath);
}

private void MakeFileDownloadable(string fileName, string imageName)
{
Response.Clear();
// Add a cookie with the imageName in it so we know we've downloaded the file contain
Response.AppendCookie(new HttpCookie("fileDownloadToken", imageName));

Response.ContentType = "application/vnd.ms-excel";
string fullFileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
Response.AddHeader("content-disposition", "attachment; filename=" + fullFileName);

using (FileStream sourceFile = new FileStream(fileName, FileMode.Open))
{
long FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, getContent.Length);

using (Stream outputStream = Response.OutputStream)
{
outputStream.Write(getContent, 0, getContent.Length);
}
}
Response.Flush();
Response.End();
}
Left by MGF on Jan 31, 2014 11:07 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Great solution! Thank you! but what happens if the cookies are disabled on the client's browser?
Left by amelie on Feb 12, 2014 4:31 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
It works fine. I had a problem in IE with my spring mvc application because I didn't set the path of the cookie and it is set by default to the current document location. In the call to the method who exports the file the document location has changed and the read of the cookie returns undefined ( $.cookie("fileDownloadToken"); ) . I solve this problem setting the root path in the cookie before add it to the response header ( cookie.setPath("/"); )
Left by Marcos Casal on Mar 04, 2014 4:17 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Works Awesome! All I needed was the jquery "polling" part and adapted the rest to my page.
I actually call an iFrame and in the C# if(!postback) set a cookie called "isLoading" = true; and start the jquery looping/checking code.
All the jquery does is check to see if the cookie= false.
Then I purposely do a postback and in the response set the isLoading cookie to "false";

When jquery sees false, continue with whatever javascript I need to do! (Like redirect, or load a popup etc etc.)

Thanks!
Left by Edmund on Mar 24, 2014 11:28 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thank you
Left by Liviu on Apr 06, 2014 1:23 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Great solution! Thanks so much for posting this.
Left by SD on Apr 11, 2014 11:57 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Exctly what I was looking for.
Good job!+
Left by Dragos on May 19, 2014 11:38 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thank you so much Buddy !!! You just nailed it out. I was scratching my head all the day for this...
Left by Sujin on Jun 12, 2014 10:13 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Please let me know how to create the value in java for 'downloadTokenValue' before adding it to the cookie?
Left by Peter on Jun 25, 2014 5:39 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thank u so much ..its really helpful for me
Left by Nipuna on Jul 13, 2014 11:20 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
works like a charm, many thanks for your work on this
Left by nat on Sep 24, 2014 8:59 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Fantastic!!! Works Great!!! For a week I was searching in vain until I found your solution. Thank you so much!
Left by John Paul on Oct 10, 2014 1:59 PM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thx ~~ you're solution is good~!
Left by korea Hwang on Nov 27, 2014 12:26 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Great! thank you!
Left by Vlad on Dec 15, 2014 3:05 AM

# re: Detecting the File Download Dialog In the Browser
Requesting Gravatar...
Thanks !
And to complete Marcos Casal's post, when you set cookie's path to '/', you have to pass the root path too when you delete it :
$.removeCookie('fileDownloadToken', { path: '/' });
Left by mpiffault on Jan 06, 2015 3:55 AM

Your comment:
 (will show your gravatar)


Copyright © Jesse Taber | Powered by: GeeksWithBlogs.net | Join free