I had used following code snippet to download a file from web server to clients machine:
(For complete description rfer previous post Downloading file using asp.net and C#)
protected void _lbtDownload_Click(object sender, EventArgs e)
{
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.SiteName.com/Downloads/Sample.zip");
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
int bufferSize = 1;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition:", "attachment; filename=Sample.zip");
Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
Response.ContentType = "application/download";
byte[] byteBuffer = new byte[bufferSize + 1];
MemoryStream memStrm = new MemoryStream(byteBuffer, true);
Stream strm = objRequest.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (strm.Read(byteBuffer, 0, byteBuffer.Length) > 0)
{
Response.BinaryWrite(memStrm.ToArray());
Response.Flush();
}
Response.Close();
Response.End();
memStrm.Close();
memStrm.Dispose();
strm.Dispose();
}
Downloading of file is working fine, but I stuck with a strange problem. After downloading the file, postback on the page stops working until the page is refreshed i.e. after downloading file, whatever you do on the page like click on a button, or click on download link again, etc, nothing will happen in the page and the page starts working only when you refresh the page.
To resolve the above issue, I played with the above code and finally found a solution that successfully worked for me.
I have created a new asp.net page “Download.aspx” and moved the above code in the “Page_Load” event of this page. Code snippet added in the code behind file of “Download.aspx”:
protected void Page_Load(object sender, EventArgs e)
{
//The following code can be used to download the file from within the web server. To download the file from remote file server, modify the below code as I have posted in my previous post.
string fileName = this.Page.Request.QueryString["FileName"].ToString();
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(”http://www.SiteName.com/Downloads/” + fileName);
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
int bufferSize = 1;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition:", "attachment; filename=Sample.zip");
Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
Response.ContentType = "application/download";
byte[] byteBuffer = new byte[bufferSize + 1];
MemoryStream memStrm = new MemoryStream(byteBuffer, true);
Stream strm = objRequest.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (strm.Read(byteBuffer, 0, byteBuffer.Length) > 0)
{
Response.BinaryWrite(memStrm.ToArray());
Response.Flush();
}
Response.Close();
Response.End();
memStrm.Close();
memStrm.Dispose();
strm.Dispose();
}
Now the code behind file of the web page where download link button “_lbtDownload” was added is modified. First the event handler routine “_lbtDownload_Click” is removed and following code snippet is added in the Page_Load event of this page.
string strDownloadPageURL = ”http://www.SiteName.com/Download.aspx?FileName=Sample.zip”;
_ibtDownload.Attributes.Add("onclick", "window.open('" + strDownloadPageURL + "', 'Download', 'menubar=0, toolbar=0, location=0, status=0, resizable=0, width=100, height=50');return false;");
Now when a user clicks on the download link, “Download.aspx” page will open in a small popup window which is closed automatcally after file is downloaded. And the postback is also working fine after file is downloaded.
Related Articles:
Downloading file using asp.net and C#