Reputation:
Here i Wrote simple c# code for Download Excel sheet from WebApi
but during the download im Getting Error As The given path's format is not supported.
please Help Me
public HttpResponseMessage GetTestFile()
{
HttpResponseMessage result = null;
// var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0");
var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0");
if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
// Serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "SampleImg";
}
return result;
}
Upvotes: 0
Views: 1327
Reputation: 648
MapPath works only for the files in your virtual directory, not the files outside your project. So always include the files that you need into your project and then use mappath. var localFilePath = HttpContext.Current.Server.MapPath("~/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0");
localFilePath //it will contain your_project_path/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0
e.g: F:\projectname\Temp\3c575edb-bf53-470c-a8b6-aa4234e796c0
Upvotes: 1