猿问

从“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:文件名、目录名或卷标的语法不正确:” [

拉风的咖菲猫
浏览 152回答 2
2回答

阿晨1998

本地文件路径不是“file://”。您可以使用本地文件路径正常读取文件var&nbsp;path&nbsp;=&nbsp;"C:\\...";然后将内容发送到客户端浏览器。如果文件不在本地计算机上,唯一的方法是使用网络共享访问它。然后您可以使用 UNC 路径,例如var&nbsp;path&nbsp;=&nbsp;@"\\Server\Path\...";

梦里花落0921

将 constantPath 更改为"\\\\A\\B\\C\\D\\"private string[] GetListOfDocumentLink(){&nbsp; &nbsp;string path = string.Empty;&nbsp; &nbsp;string constantPath = "\\\\A\\B\\C\\D\\";&nbsp; &nbsp;string folderName = string.Empty;&nbsp; &nbsp;string year = string.Empty;&nbsp; &nbsp;// determine folderName and year.&nbsp;&nbsp;&nbsp; &nbsp;path = constantPath&nbsp; &nbsp; &nbsp; &nbsp; + Path.DirectorySeparatorChar.ToString()&nbsp; &nbsp; &nbsp; &nbsp; + folderName&nbsp; &nbsp; &nbsp; &nbsp; + Path.DirectorySeparatorChar.ToString()&nbsp; &nbsp; &nbsp; &nbsp; + year;&nbsp; &nbsp; &nbsp; &nbsp; var filter = Berichtsnummer + "*.pdf";&nbsp; &nbsp; &nbsp; &nbsp; string[] allFiles = Directory.GetFiles(path, filter);&nbsp; &nbsp; &nbsp; &nbsp; return allFiles;}现在您可以发送pathtoDownloadDocument方法:public async Task<IActionResult> DownloadDocument(string path){&nbsp; &nbsp;byte[] berichtData = null;&nbsp; &nbsp;FileInfo fileInfo = new FileInfo(path);&nbsp; &nbsp;long berichtFileLength = fileInfo.Length;&nbsp; &nbsp;FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);&nbsp; &nbsp;BinaryReader br = new BinaryReader(fs);&nbsp; &nbsp;berichtData = br.ReadBytes((int)berichtFileLength);&nbsp; &nbsp;return File(berichtData, MimeTypeHelper.GetMimeType("pdf"));}
随时随地看视频慕课网APP
我要回答