Reputation: 1309
im trying to get a file outside of my application folder to download...
public FilePathResult DownloadFile(Guid id, string dateiname)
{
string pfad = @"D:\wwwroot\portal_Daten\";
return File(pfad + dateiname, "application/pdf", dateiname);
}
Errormessage: is a D:\wwwroot\portal_Daten\8/0/6/a/e/974-aaca-426c-b7fc-e6af6c0fb52e/oeffentlich is a physical path, but it was a virtual path expected.
Why can't this work with a physical path? How can i make this to a virtual path?
Regards, float
Upvotes: 0
Views: 4301
Reputation: 3326
The way I have handled this in the path is to use a custom file download http handler (for an asp.net webforms application) and you could use the same here. You could even construct a new sub-class of ActionResult which might net you the same result.
The way I did this was to create an implementation of IHttpHandler, handle the request and return the file. This way you're not restricted to using virtual paths and, as long as your web server security configuration allows, you will be able to access any file on your system and return that to the browser.
Something like:
public class MyFileHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
string filePath = Path.Combine(@"d:\wwwroot\portal_daten", context.Request.QueryString["dateiname"]);
context.Response.ContentType = "application/pdf";
context.Response.WriteFile(filePath);
}
}
A stripped down example with no checking, but that's for you to fill in. Then register the handler in your web.config:
<handlers>
<add name="MyFileHandler" path="file.axd" type="MvcApplication4.Models.MyFileHandler" verb="GET" />
</handlers>
You will of course have to rename classes/namespaces to suit yourself. The actual web link to the file then becomes:
http://[domain]/file.axd?dateiname=mypdf.pdf
Where [domain] is your domain name/localhost or whatever you're using.
Upvotes: 2
Reputation: 8278
you need to use Server.MapPath and give it a file location so that it can map the path to a relative directory on the server
so something like
public FilePathResult DownloadFile(Guid id, string dateiname)
{
string pfad = Server.MapPath(@"D:\wwwroot\portal_Daten\");
var filePath = Path.Combine(pfad, dateiname);
return File(filePath , "application/pdf", dateiname);
}
should work
Upvotes: 0