Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to download resume file from folder nameed as strjobID and file name is stored in CommandArgument of grid view , but while execution I am Getting this Exception

'D:/AdminApp/Jobportals/Utils/ApplicantFiles/27/directory.txt' is a physical path, but a virtual path was expected.

Please suggest some solution for this
C#
try
{
   Button Button1 = (Button)sender;
    
   string strFilename = ((Button)sender).CommandArgument;
   string strURL = Server.MapPath(@"" + "~/Utils/ApplicantFiles/" + strjobID + "/" + strFilename);
   WebClient req = new WebClient();
   HttpResponse response = HttpContext.Current.Response;
   response.Clear();
   response.ClearContent();
   response.ClearHeaders();
   response.Buffer = true;
   response.AddHeader("Content-Disposition", "attachment;filename=\"" + strURL + "\"");
   byte[] data = req.DownloadData(Server.MapPath(strURL));
   response.BinaryWrite(data);
   response.End();
}
Posted
Updated 12-Dec-12 22:18pm
v2

1 solution

I assume this line is the error:

byte[] data = req.DownloadData(Server.MapPath(strURL));

You did MapPath on strURL already. So, it's already a local path, but you pass it to a method that expects a virtual path to convert.
 
Share this answer
 
Comments
kishanthakar 13-Dec-12 5:45am    
Your suggestion worked ...... I got solution

Thank you Christian Graus
Arafatcse 6-Jan-14 8:28am    
is it like that
byte[] data = req.DownloadData(Server.MapPath("~/SomePdf.Pdf"));
kishanthakar 13-Dec-12 5:50am    
still one help if you can

when my file get downloaded it name is like my whole path foe e.g.
D-AdminAppJobportalsUtilsApplicantFiles20directory

is there be any way by which i can change the downloaded file name?
Christian Graus 13-Dec-12 14:05pm    
You can change it any way you want. You write the filename as strURL in to the header. You can use Path.GetFileName to get just the file name, or create any file name you like, just put it in to that header.
ErikVabo 28-Jul-14 6:17am    
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
string Filepath = e.CommandArgument.ToString();
string path = MapPath(Filepath);
byte[] bts = System.IO.File.ReadAllBytes(path);


Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-type", "application/octect-stream");
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; Filepath=" + Filepath);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
}
}

Having the same problem, how can I fix this?

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