I have an ASP.Net rest Project, How can I upload files to a shared Network folder (Eg: \FileServer\Files). I should be able to access the file from the folder by calling GetFile api and passing fileName to it only if the user is authenticated.
User should NO be able to access the files directly using link.
The Api should be responsible to retrieve and stream file to the authenticated user.
What I have already Tried as follow
My File Upload (See Code Below)works on local network and files after deployed to IIS
Route("api/[controller]/[Action]")] [HttpPost] public async Task<IActionResult> UploadFile(IFormFile file) { var filePaths = new List<string>(); if (file.Length > 0) { var currentDirectory = Environment.CurrentDirectory; var filePath = "\\FileServer\\Files\\" +"FILE_" + file.FileName; filePaths.Add(filePath); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } } return Ok(new { count = "na", filePaths }); }
My File Retrieve as below also fails after deploying to IIS
[Route("api/[controller]/[Action]")] [HttpGet("filename")] public async Task<IActionResult> ReturnStreamAsync1(string fileName) { String rootSERVER = "\\FileServer\\File"; IFileProvider provider = new PhysicalFileProvider(rootSERVER); IFileInfo fileInfo = provider.GetFileInfo(fileName); var readStream = fileInfo.CreateReadStream(); return File(readStream, "audio/mp3", fileName); }
Any Help will be highly appreciated