首页 > 解决方案 > 从“file://A/”获取文件

问题描述

如何从与本地计算机相同的外部路径获取文件"file://A/B/C/D/" 我可以访问“file://”的路径但用户无权访问。现在我想从“file://A/B/C/D/”中读取一些文件并让用户下载。

我该怎么做?

(当前目录为“ https://localhost:44331/ ”)

public async Task<IActionResult> DownloadDocument(string berichtsnummer)
{
   var constantPath = "file://A/B/C/D/";
   using (FileStream fileStream = System.IO.File.OpenRead(constantPath))
   {

      MemoryStream memStream = new MemoryStream();
      memStream.SetLength(fileStream.Length);

     fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
     return File(fileStream, "application/octet-stream");
   }
}

当我单击下载链接时,我收到此错误:

“IOException:文件名、目录名或卷标的语法不正确:” [异常[1]

路径“file://A/B/C/D/”的视图:

文件路径

标签: c#asp.net-coreasp.net-core-2.0

解决方案


本地文件路径不是“file://”。您可以使用本地文件路径正常读取文件

var path = "C:\\...";

然后将内容发送到客户端浏览器。

如果文件不在本地计算机上,唯一的方法是使用网络共享访问它。然后您可以使用 UNC 路径,例如

var path = @"\\Server\Path\...";

推荐阅读