Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using Response.WriteFile method for downloading an excel sheet from a desired location to client machine.Below is the code:-
C#
// Checking if file exists
                if (myfile.Exists)
                {
                    // Clear the content of the response  
                    HttpContext.Current.Response.ClearContent();

                    // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

                    // Add the file size into the response header
                    HttpContext.Current.Response.AddHeader("Content-Length", myfile.Length.ToString());

                    // Set the ContentType
                    HttpContext.Current.Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

                    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
                    HttpContext.Current.Response.WriteFile(myfile.FullName);

                    // End the response
                    HttpContext.Current.Response.End();
                }
private string ReturnExtension(string fileExtension)
        {
            switch (fileExtension)
            {
                case ".xlsx":
                    return "application/excel";
                default:
                    return "application/octet-stream";
            }
        }


I want that once the download pop up will appear and asking for Save/Open/Cancel and the end user select his option then after completion of that task a post back happen to the webpage from where it is been invoked. Suppose if user click save then the file get saved and after saving a post back will happen.Same if user select open then the file will be opened and then a post back happen to the webpage from where this Download function is invoked.
Posted
Comments
dimpledevani 2-Mar-12 5:56am    
well you can redirect it back to your desired page after your task complete
Dharmenrda Kumar Singh 2-Mar-12 6:00am    
I had tried this but once i am redirecting to the same page , the download pop up is getting disappear.

it is very easy man you can add button with visible = "false" property and with
OnClick="button1_Click" property
C#
button1_Click((object sender, System.EventArgs e)
{
   //and leave it empty;
}


the after you finish your code at the end call this button event
as follows:
C#
button1_Click(null,null);


ex: as you say if user select open then the file will be opened and then a post back happen ok at the end of this code (and the file opened)call the empty function...easy

regard
 
Share this answer
 
i have a same problem plz help me
solution1 does't work
 
Share this answer
 
<pre lang="c#"><pre lang="text"><pre lang="text"></pre></pre></pre>All You Can Do.You Should Use JavaScript Method Before Exporting Your Excel.To Call The Server Side Event. Or If You Can Handle it in Your JavaScript It Will Better..
 
Share this answer
 
v2

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