3

I am developing a website where I am uploading document and stored in folder. Uploading document works fine but download code not works. I need to download file from folder.

protected void btnDownload_Click(object sender, EventArgs e)
{        
         lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString();         
         if (lblresume.Text != string.Empty)        
         {
             string filePath = lblresume.Text;             
             Response.ContentType = "doc/docx";             
             Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");              
             Response.TransmitFile(Server.MapPath(filePath));             
             Response.End();         
         }    
}
3
  • Try fully-qualifying the path of your file. Commented Mar 14, 2014 at 11:46
  • "~/Student_Resume/" + fuResume.FileName.ToString is it fully-qualifying path Commented Mar 14, 2014 at 12:02
  • Path should be physical. Commented Mar 14, 2014 at 12:04

3 Answers 3

9

try this

protected void btnDownload_Click(object sender, EventArgs e)
{        
     lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString();         
     if (lblresume.Text != string.Empty)        
     {
         WebClient req = new WebClient();
         HttpResponse response = HttpContext.Current.Response;
         string filePath = lblresume.Text;               
         response.Clear();
         response.ClearContent();
         response.ClearHeaders();
         response.Buffer = true;
         response.AddHeader("Content-Disposition", "attachment;filename=Filename.extension");
         byte[] data = req.DownloadData(Server.MapPath(filePath));
         response.BinaryWrite(data);
         response.End();                   
     }    
}
Sign up to request clarification or add additional context in comments.

5 Comments

byte[] data = req.DownloadData(filePath); In this line iam geting Could not find a part of the path error.
This line takes file name from fileupload tool not from lblresume byte[] data = req.DownloadData(filePath);
try using Server.MapPath(filePath)
sir Could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\Student_Resume\'. This error comes
which is the location of Student_Resume folder. Is it in root. then you can use string path=Server.MapPath("~/Student_Resume/") + @"\resume.docx"; and in this place use byte[] data = req.DownloadData(path);
1

Filepath in

Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); 

Should be physical path; and not styart with: ~.
Content-type should be "application/ms-word"

4 Comments

Filepath should be filename+ext without ~. This is the filename that will appear by default when download file dialogbox is shown
how to access the filename from the folder to download. Here it takes filename from fileupload not form folder
yes sir but this line takes file name from fileupload tool not from lblresume Response.TransmitFile(Server.MapPath(filePath));
Convert to filePath to filename+ext using Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(Server.MapPath(filePath)) + "\"");
0
protected void btnDownload_Click(object sender, EventArgs e)
{        
     lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString();         
     if (lblresume.Text != string.Empty)        
     {
         string filePath = lblresume.Text;             
         Response.ContentType = "doc/docx";             
         Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fuResume.FileName.ToString() + "\"");              
         Response.TransmitFile(Server.MapPath(filePath));             
         Response.End();         
     }    
}

Try this code. This should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.