0

FileController.cs

public IActionResult GetFileByName(string name)
  {
    return _file.GetFile(name);
  }

FileRepository.cs

  public FileStreamResult GetFile(string name)
  {
  
    string path = "C:\\"
    FileInfo fileInfo = new fileInfo(path);
    using FileStream fileStream = fileInfo.Open(FileMode.Open);
    return new FileStreamResult(fileStream, "application/pdf")
                {
                    FileDownloadName = "bill.pdf"
                };
}

////In Response exception message ###System.ObjectDisposedException: Cannot access a disposed object.

  • 1
    `using` causes `Dispose` to be called when the end of scope for that variable is reached. – Paweł Łukasik Mar 16 '22 at 07:51
  • What are you trying to do? Why a `FileStreamResult` when you have a file path? The *correct* way would be to return a ``PhysicalFileResult` with the absolute file path you want to return – Panagiotis Kanavos Mar 16 '22 at 07:56

0 Answers0