Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
2.58/5 (4 votes)
See more:
C#
string path = (@"D:\Doc\Offer letter.pdf"); //get physical file path from server
    string name = Path.GetFileName(path); //get file name
    string ext = Path.GetExtension(path); //get file extension
    string type = "";

    // set known types based on file extension
    if (ext != null)
    {
        switch (ext.ToLower())
        {
            case ".htm":
            case ".html":
                type = "text/HTML";
                break;

            case ".txt":
                type = "text/plain";
                break;

            case ".GIF":
                type = "image/GIF";
                break;

            case ".pdf":
                type = "Application/pdf";
                break;

            case ".doc":
            case ".rtf":
                type = "Application/msword";
                break;
        }
    }

    Response.AppendHeader("content-disposition", "attachment; filename=" + name);

    if (type != "")
        Response.ContentType = type;
    Response.WriteFile(path);

    Response.End(); //give POP to user for file downlaod



i have this code for downloading file from server to client. But this code not yet all showing any dailogue box for doanload and save.Please any one help me.
Posted
Updated 11-Feb-19 2:42am
Comments
Nirav Prabtani 25-Apr-17 1:09am    
Your code is working fine. I have checked it.

u can use this code...

HTML
 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 […]
}
 
Share this answer
 
Though the post is very old, submitting for any new user having the same kind of issue:

If your problem is just "not getting the dialog box", then it's browser dependent and if it's already checked as 'Do not ask again' kind of selection, it wont appear again. You need not worry about this.

Same code may download the files for another user with dialog box with save button. Check it in different browser ie. Chrome and Firefox.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900