从 "file://A/" 获取文件

Getting a file from "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");
   }
}

当我点击下载 link 时,出现此错误:

"IOException: The syntax for filename, directory name, or volume label is incorrect:" [

路径视图"file://A/B/C/D/":

本地文件路径不是"file://"。使用本地文件路径as

即可正常读取文件
var path = "C:\...";

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

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

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

将 constantPath 更改为 "\\A\B\C\D\"

很重要
private string[] GetListOfDocumentLink()
{
   string path = string.Empty;
   string constantPath = "\\A\B\C\D\";

   string folderName = string.Empty;
   string year = string.Empty;
   // determine folderName and year.  

   path = constantPath
        + Path.DirectorySeparatorChar.ToString()
        + folderName
        + Path.DirectorySeparatorChar.ToString()
        + year;

        var filter = Berichtsnummer + "*.pdf";

        string[] allFiles = Directory.GetFiles(path, filter);
        return allFiles;
}

现在您可以将 path 发送到 DownloadDocument 方法:

public async Task<IActionResult> DownloadDocument(string path)
{
   byte[] berichtData = null;
   FileInfo fileInfo = new FileInfo(path);
   long berichtFileLength = fileInfo.Length;
   FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
   BinaryReader br = new BinaryReader(fs);
   berichtData = br.ReadBytes((int)berichtFileLength);

   return File(berichtData, MimeTypeHelper.GetMimeType("pdf"));
}