Reputation: 11
This is my code for downloading text file. But the server.transfer method is not able to resolve that path. It is giving "is not a valid virtual path error"
string filePath = @"D:/BCPResult/Cust_File.t`enter code here`xt";
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition",
"attachment; filename=" + filePath);
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
Please guide me...
Upvotes: 0
Views: 13413
Reputation: 1
If You Are trying to access a file from the Remote URL or External URL like (https://www.example.com/)
then you have to Use for the web client like this
string url= "this is the URL of the file from where you want to access the file"
System.Net.WebClient webClient = new System.Net.WebClient();
byte[] bytes = webClient.DownloadData(url);
if (mimetype != null)
{
context.Response.ContentType = mimetype;
}
else
{
context.Response.ContentType = "Application/pdf";
}
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.BinaryWrite(bytes);
context.Response.End();
Upvotes: 0
Reputation: 8146
If your file path is not related to server you do not need Server.MapPath
.
Also if you run your code in windows, path separator is \, not /.
This code must work:
string filePath = @"D:\BCPResult\Cust_File.txt";
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition", "attachment; filename=" + filePath);
Response.TransmitFile(filePath);
Response.End();
Upvotes: 2
Reputation: 712
Use '\' (backslash) instead '/'.
string filePath = @"D:\BCPResult\Cust_File.txt";
or
string filePath = "D:\\BCPResult\\Cust_File.txt";
Upvotes: 1