Saturday, April 5, 2014

How to download a file from physical drive in asp.net?

void getexcel()
{
        try
        {
            Context.Response.Buffer = false;
            FileStream file = null;
            byte[] mybuff = new byte[1024];
            long count;
//Specify the drive letter where your files are saved and append the file name according your requirement 
            file = File.OpenRead("L:\\" + Session["dept"].ToString() + "\\" + Session["FileName"].ToString());
            if (file != null)
            {
                while ((count = file.Read(mybuff, 0, mybuff.Length)) > 0)
                {
                    Response.ContentType = "application/pdf";
                    Response.BinaryWrite(mybuff);
                }
            }
            else
            {
                Response.Write("File not found.");
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
}

No comments:

Post a Comment