Click here to Skip to main content
15,886,661 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have made a functionality where user selects something to download and get it in a new tab easily with the file save open dialogue box. It is nice and acceptable but the cross browser issue prevails here. Because in Mozilla the new tab stays as long as the file is not fully downloaded but in chrome the page persists even if the download is complete. Sometime it takes time to download the file(.csv) so I created a page that has HTML mark up as please do not close this window but however it never gets properly done. I am getting a error : request eroor 404, file was not found something like that and thus it is preventing the things to happen correctly. Could you please suggest me something out of it ???
Here is my code :
C#
 Session["download_file_link"] = strResult;
                string strDownloadPageURL = strResult;
                ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'Download.aspx', null, 'height=400,width=800,status=yes,toolbar=no,menubar=no,location=no' );", true);

/* than on download page */

public void Download_file_for_user(string strFilename)
    {
        System.Net.HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.cybexexim.net/Downloads/" + strFilename);
        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();<-- error here
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.AppendHeader("Content-Disposition:", "attachment; filename=" + strFilename + "");
        Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
        Response.ContentType = "application/download";
        Response.Close();
        Session.Remove("download_file_link");
        Response.End();
    }


calling this method on page load. It still doesnt allows to open this page properly and file doesnt gets download. However If I use my download string directky on javascript used above than no prob exists but download.aspx page
never comes. Please help !
Posted
Updated 6-Aug-12 19:58pm
v3

1 solution

Try something like this.

C#
 System.Net.HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.cybexexim.net/Downloads/" + strFilename);
request.ContentType = "application/json; charset=utf-8";
request.Credentials = CredentialCache.DefaultCredentials;
try
{
    response = (HttpWebResponse)request.GetResponse();

    // perform normal processing
}
catch(WebException ex)
{
    // are you sure you just want to swallow the exception like this?
    continue;
}




if it still throws error, catch the error and let us know the error message.
 
Share this answer
 
v2
Comments
Taresh Uppal 7-Aug-12 3:12am    
hello, I really appreciate your help but this is not what I am looking for. I said that I am able to download files but still I want that the tab in which new page open for download should be designed and customized by me having something like "Please do not close this tab etc etc..." So that if server takes time to render the file on machine user don't close the tab. In order to achieve that I am using all this and not getting successful. I tried your code it said the URL is empty..:(
Taresh Uppal 7-Aug-12 3:13am    
The java script to open into anew tab if I pass the download link of the file it works properly but open a blank page not a customized one and if I pass the required parameters to the Download.aspx page than i get these errors. Now I guess I am more specific...
Santhosh Kumar Jayaraman 7-Aug-12 3:13am    
You have to make changes in the code.

WebRequest.Create(stringBuilder.ToString()) as HttpWebRequest;

Here you have to use your code.
Santhosh Kumar Jayaraman 7-Aug-12 3:14am    
Yes. I have given the code to donwload files in new page, not through javascript.and i guess thats what you wanted
Taresh Uppal 7-Aug-12 3:25am    
but it gives an exception that URL is empty in this line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900