Difficulties figuring out how to initiate download of files after their creation RRS feed

  • Question

  • User88462765 posted


    I have a collection of files which are constantly being created on a remote server, but not uploaded to my web server until requested by a client.  When a client requests a file, I want to pull it onto my web werver, whilst checking for it's creation using a FileSystemWatcher.  Once it is created, I wish to initiate a download by doing:



                Response.ContentType = "text/plain";
                Response.AddHeader("content-disposition", "attachment; filename=" + channelAFileInfo.Name);
                Response.AddHeader("content-length", channelAFileInfo.Length.ToString());
    The problem is that I want checking for the file to continue in the background, event if a user navigates off the page after requesting a file.  Since Page.HttpResponse is only available in code-behind, how can I do this?
    I have tried using a separate class, created in the session state, and passing this.Response into it from the initial page so that I have a reference to it, but of course this is invalid too.  My other idea is to open up a popup window or a hidden window to handle the http response.  I think this option would work, but it seems messy to me.
    If all else fails, I could just say: "stay on the page until the download completes" but I would prefer not to.
    I would appreciate any help with this, as I am pretty confused as to what to do. I can't find any documentation of similar problems.
    Many Thanks,
    Friday, December 15, 2006 11:44 AM

All replies

  • User-1348799698 posted

    the web is stateless.  There is no way to capture a response object to use later.  If the file is not on your server, you cannot serve it later. 

    The way I have handled this is to have your web page queue the request for the file and let the user know they will receive a notice when the file is available.  Once the file is available, send the user an email using your filesystemwatcher (create a separate application or set something up with file dependencies on your web application).  The email should have a link to the file in order to download it or a page that initiates the download and then removes it as necessary.

    good luck,


    Friday, December 15, 2006 5:23 PM
  • User88462765 posted

    Hi JJ,

    Thanks for your reply.

    I now have a clearer idea of how I am goiong to tackle this.  You are correct that the web is stateless, and therefore, if the user navigates away from the "Download_Request" page, there is nothing I can do to let them download the file, since that particular Request-Response interaction is over.  If the file the user requests is not present on the web server, I will use javascript to open a page, DownloadData.aspx, in a popup window.  My javascript function will post parameters to this function to specify the file to get from the remote server.  I will use a class containing a FileSystemWatcher within my application state to watch for created files in response to requests from all users.  This will also have the benefit of not replicating requests for the same files from different users.  During the time when the file is being retrieved from the remote server, the popup window will display a ProgressBar-type control and a message "Downloading file... please do not close this window".  Once the file is available, the popup will either close atumatically (not sure how to do this) or display "Download completed, please select the file for download to your computer".  The user can then close the popup, and re-click the file link to download the file.


    What do you think of this mechanism? Would it be affected by popup blockers? Would it be better to open a new window - "target=_blank" - rather than a popup? How could a normal window like this be resized and have address bar, etc removed?


    Thanks for taking the time to answer my post, much appreciated!


    Friday, December 15, 2006 6:10 PM
  • User-1348799698 posted

    take a look at the window.open javascript command.  There are parameters you can use to size it and set some of the options.  you will not be able to interact with the window once the window is done.  You will have to have a javascript keep-alive script running on the client side of the window.  You can set the javascript to call a postback every so often (settimeout / doPostback) in order to check if the file is available.  if it is, redirect the page to the download.

    The popup window will work better so the user does not have to worry about staying on the same page.  


    good luck.



    Saturday, December 16, 2006 6:15 PM
  • User88462765 posted


    I am back onto this after working on other parts of the site. I am inexperienced in javascript, so my attempts at this are not so good. I am thinking about using either the  ClientScript.RegisterClientScriptBlock or ClientScript.RegisterStartupScript functions, but I don't know which would be better. My idea is to have a javascript function - say "checkfunc()" - which runs in the page_load event of my popup window, this calls a C# method to check if the files are ready. If not, it resets the timeout and the process starts again while it writes a message to a Label on the page; if they are ready, it writes an html link to the page which the user can click to open the files in another window.

    My problems are:

    I have POST values in the url - will these be preserved with window.settimeout? (I am assuming this reloads the page but I could be wrong.)

    How can I call a C# function from a javascript function and vice versa?


    It would be ideal if someone could post example source or pseudo code to get me on the right track![Idea]




    Sunday, January 21, 2007 4:53 PM
  • User113421904 posted


    You can't call server side C# code form client side javascript code directly. However you can let javascript run automatically when page is loaded in your web browser and do the postback to the server, then the server will trigger certain page event. You can add event handler to the event. The entire procedure is similar as server side code called by client side code. And parameter can be past by HTTP GET or HTTP POST.

    Hope it helps


    Tuesday, January 23, 2007 1:12 AM
  • User-1348799698 posted

    Here is a link describing how to generate a script to post back to the server:


    good luck.



    Tuesday, January 23, 2007 1:49 PM