x19
x19

Reputation: 8783

Getting a file from "file://A/"

How can I get a file from external path same as "file://A/B/C/D/" In local machine I have access to the path of "file://" but the user has not access. Now I want to read some files from "file://A/B/C/D/" and make downloadable for user.

How can I do it?

(current directory is "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");
   }
}

when I click to download link, I get this error:

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

A view of path "file://A/B/C/D/":

File path

Upvotes: 0

Views: 163

Answers (2)

x19
x19

Reputation: 8783

That's important to change the constantPath to "\\\\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;
}

Now you can send the path to DownloadDocument method:

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

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56717

A local file path is not "file://". You can read the file normally using the local file path as

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

and then send to content to the client browser.

If the file is not on the local machine, the only way is to access it using a network share. You can then use UNC paths, like

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

Upvotes: 2

Related Questions