mvc下载app文件夹外的文件

发布于 2024-10-12 11:10:50 字数 416 浏览 3 评论 0原文

我试图在我的应用程序文件夹之外下载一个文件...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-10-19 11:10:50

我在路径中处理此问题的方法是使用自定义文件下载 http 处理程序(对于 asp.net webforms 应用程序),您可以在此处使用相同的方法。您甚至可以构造一个 ActionResult 的新子类,这可能会得到相同的结果。

我这样做的方法是创建 IHttpHandler 的实现,处理请求并返回文件。这样,您就不再局限于使用虚拟路径,只要您的 Web 服务器安全配置允许,您就可以访问系统上的任何文件并将其返回到浏览器。

类似于:

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);
  }
}

一个没有检查的精简示例,但需要您填写。然后在您的 web.config 中注册处理程序:

<handlers>
  <add name="MyFileHandler" path="file.axd" type="MvcApplication4.Models.MyFileHandler" verb="GET" />
</handlers>

您当然必须重命名类/命名空间以适合自己。该文件的实际 Web 链接将变为:

http://[domain]/file.axd?dateiname=mypdf.pdf

其中 [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:

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.

没︽人懂的悲伤 2024-10-19 11:10:50

您需要使用 Server.MapPath 并给它一个文件位置,以便它可以将路径映射到服务器上的相对目录,

这样

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);
}

应该可以工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文