mvc下载app文件夹外的文件
我试图在我的应用程序文件夹之外下载一个文件...
public FilePathResult DownloadFile(Guid id, string dateiname)
{
string pfad = @"D:\wwwroot\portal_Daten\";
return File(pfad + dateiname, "application/pdf", dateiname);
}
错误消息:是 D:\wwwroot\portal_Daten\8/0/6/a/e/974-aaca-426c-b7fc-e6af6c0fb52e/oeffentlich 是一个物理路径,但它是预期的虚拟路径。
为什么这不能用于物理路径?我怎样才能使它成为虚拟路径?
问候, 漂浮
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在路径中处理此问题的方法是使用自定义文件下载 http 处理程序(对于 asp.net webforms 应用程序),您可以在此处使用相同的方法。您甚至可以构造一个 ActionResult 的新子类,这可能会得到相同的结果。
我这样做的方法是创建 IHttpHandler 的实现,处理请求并返回文件。这样,您就不再局限于使用虚拟路径,只要您的 Web 服务器安全配置允许,您就可以访问系统上的任何文件并将其返回到浏览器。
类似于:
一个没有检查的精简示例,但需要您填写。然后在您的 web.config 中注册处理程序:
您当然必须重命名类/命名空间以适合自己。该文件的实际 Web 链接将变为:
其中 [domain] 是您的域名/localhost 或您正在使用的任何内容。
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:
A stripped down example with no checking, but that's for you to fill in. Then register the handler in your web.config:
You will of course have to rename classes/namespaces to suit yourself. The actual web link to the file then becomes:
Where [domain] is your domain name/localhost or whatever you're using.
您需要使用 Server.MapPath 并给它一个文件位置,以便它可以将路径映射到服务器上的相对目录,
这样
应该可以工作
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
should work