ReadAllBytesAsync(path) 路径始终与 contentrootpath 组合

发布于 2025-01-15 02:48:26 字数 834 浏览 2 评论 0原文

我想从虚拟路径目录返回一个文件,目前这是我在 C# asp net core 中的代码。

     var path = GetConfig.AppSetting["VirtualDirectoryPath:PathLocal"] + "/" + 
                _.data.Downloadpath;
     var provider = new FileExtensionContentTypeProvider();
     if (!provider.TryGetContentType(path, out var contentType))
     {
         contentType = "application/octet-stream";
     }

     var bytes = await System.IO.File.ReadAllBytesAsync("http://website.com/foldername/foldername/filename.pdf");

     return File(bytes, contentType, Path.GetFileName(path));

下载路径包含“foldername/filename.pdf”,虚拟目录路径包含本地主机域,我尝试对路径进行硬编码,但它不断返回“D://folder/folder/http://website.com/foldername/文件夹名/文件名.pdf”。我不知道为什么 url 与 contentrootpath 结合在一起,有人可以帮助我吗?

实际上,我还想从我已经在iis中创建的虚拟目录中检索虚拟目录路径作为url,以便我可以从url下载文件。我不知道如何更好地编码,如果您也能帮助我,我将非常感激。 TIA。

I want to return a files from virtual path directory, currently this is my code in C# asp net core.

     var path = GetConfig.AppSetting["VirtualDirectoryPath:PathLocal"] + "/" + 
                _.data.Downloadpath;
     var provider = new FileExtensionContentTypeProvider();
     if (!provider.TryGetContentType(path, out var contentType))
     {
         contentType = "application/octet-stream";
     }

     var bytes = await System.IO.File.ReadAllBytesAsync("http://website.com/foldername/foldername/filename.pdf");

     return File(bytes, contentType, Path.GetFileName(path));

the DownloadPath contains 'foldername/filename.pdf', and the virtual directory path contains the localhost domain, i tried to hardcode the path but it keeps returning "D://folder/folder/http://website.com/foldername/foldername/filename.pdf". I don't know why the url combined with the contentrootpath, can someone help me?

Actually, i also want to retrieve a virtual directory path as url from the virtual directory that i already created in the iis so that i can download the files from the url. I don't know how to code better, if you can help me with this too i'd very much appreciate it. TIA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

那一片橙海, 2025-01-22 02:48:26

最终,问题在于您似乎相信 File.ReadAllBytesAsync 从网络下载文件,但事实并非如此。它仅读取本地文件系统上的文件。

由于不支持 http:// 文件,因此它们不被视为 rooted,因此它会连接到“当前文件夹”,您就会看到所看到的内容。

也永远不要像这样连接路径,这就是 Path.Combine 用于。

Ultimately, the problem is that you seem to believe File.ReadAllBytesAsync downloads files from the web, it does not. It only reads files on your local file system.

And because http:// files aren't supported, they're not considered to be rooted, and so it gets concatenated to the "current folder" and you get what you see.

Also never concatenate paths like that, that's what Path.Combine is for.

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