Hi following is the code which i have used to download the uploaded file.
private void DownloadFile(object sender)
{
LinkButton lnkBut = (LinkButton)sender;
string fileName = lnkBut.Attributes["fileName"];
String filePath = Server.MapPath("~/FilesUploaded/") + fileName;
FileWebRequest objRequest = (FileWebRequest)WebRequest.Create(filePath);
FileWebResponse objResponse = (FileWebResponse)objRequest.GetResponse();
int bufferSize = 1;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + lnkBut.Text);
Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
Response.ContentType = this.ResponseContentType(fileName); // the method supplies the contenttype based on file extention
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();
}
The above method works fine with the firefox and IE , i am able to download all types of files like zip,txt,xls,xlsx,doc,docx
but when i download the same file from googlechrome , instead of actual zip file, the default.aspx page has been downloaed
Please any one tell me what is the way to get the actual file in google chrome.
Loading
Jignesh TrivediPosted May 21, 2012, 8:16 AM
try with following change in youe code.
ie. include your file path with AppendHeader
Response.AddHeader("Content-Disposition",
"attachment; filename=myfile.txt; size=" + objResponse.Length.ToString());
also remove Response.Flush() in while loop only add it after while loop.
hope this help.
Pramod Kumar NandagiriPosted May 22, 2012, 3:17 AM