1

I'm trying to write an action that will serve files. What I've tried (File method comes from ControllerBase):

string fileServer = @"\\FileServer\Attachments";
[HttpGet("{fileName}")]
public async Task<IActionResult> Index([FromRoute] string fileName)
{
    var filePath = Path.Combine(fileServer, fileName);

    try
    {
        var fs = new FileStream(filePath, FileMode.Open);
        return File(fs, "application/octet-stream");
    }
    catch (Exception e)
    {
        return BadRequest(new { ExitMessage = e.Message });
    }
}

Alternative approach as suggested by someone else:

try
{
    var memory = new MemoryStream();
    using (var stream = new FileStream(filePath, FileMode.Open))
    {
        await stream.CopyToAsync(memory);
    }
    memory.Position = 0;

    return File(memory, "application/octet-stream");
}

Both work fine but when a second request is triggered whilst the first request didn't finish yet they get

System.IO.IOException
The process cannot access the file '\FileServer\Attachments{fileName}' because it is being used by another process.

How can I serve the same file simultaneously?

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Pawel
  • 891
  • 1
  • 9
  • 31
  • Thank you @Nkosi! Using following overload fixed the problem: ```var fs = new FileStream(serverLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);``` – Pawel Jan 06 '21 at 15:15

0 Answers0