ali raza

C#, .Net , and my thoughts

downloading file in c#

private void DownloadFile(String strFileName){FileStream fs;

string strContentType;string strPath=TaskManagementTool.Config.Path.ToString();

//Dim strFileName As String = Request.QueryString(”DocumentFile”)fs = File.Open(Server.MapPath(strPath + strFileName), FileMode.Open);

Byte[] bytBytes = new byte[fs.Length] ; // Write the stream to the byte array

fs.Read(bytBytes, 0, Convert.ToInt32(fs.Length)); // Close the file stream to release the resource

fs.Close();Response.AddHeader(“Content-disposition”, “attachment; filename=”+strFileName);

// Next we need to add some header information.// These headers will tell the browser what it needs to do

// with the content we’re serving

Response.ContentType = “application/octet-stream”;// Now our headers are added, we can serve the content.// To do this, we use the BinaryWrite() method of the server object

// This successfully streams our external file to the user,

// despite the fact that the file doesn’t exist

// anywhere inside the web application

Response.BinaryWrite(bytBytes);// Call Response.End() so that no more// content goes through to the client.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: