Thursday, September 01, 2011

c# aspnet download file from server side

string text1 = this.Server.MapPath("~/" + [FileFolder]);
if (Directory.Exists(text1))
{
string text2 = text1 + @"\[File Name with extension]";
if (System.IO.File.Exists(text2))
{
FileStream stream1 = new FileStream(text2, FileMode.Open, FileAccess.Read);
byte[] buffer1 = new byte[((int)stream1.Length) + 1];
stream1.Read(buffer1, 0, (int)stream1.Length);
stream1.Close();

//Writes the file out the Response object
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment;filename=\"[File Name with extension]\";");
Response.WriteFile(text2, true);
Response.ContentType = "application/x-zip-compressed";
Response.BinaryWrite(buffer1);
Response.End();
}
}

No comments: