Advertisements

How to download files using Asp.net, C#




 protected void lnkfilepath_Click(object sender, EventArgs e) // ur link button
    {
        string filename = lnkfilepath.Text;
        string Filpath = Server.MapPath(“~/Attachments/” + filename);
        DownLoad(Filpath);

    }
    public void DownLoad(string FName)
    {
        string path = FName;
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader(“Content-Disposition”, “attachment; filename=” + file.Name);
            Response.AddHeader(“Content-Length”, file.Length.ToString());
            Response.ContentType = “application/octet-stream”; // download all types of files..
            Response.WriteFile(file.FullName);
            Response.End();

        }
        else
        {
            Response.Write(“This file does not exist.”);
        }

    }

 


Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: