0

I found a post about using disposable object with the download action. However I found the file I created was disposed before the download action therefore it returned "Could not find file..." error. Any idea? Thanks you.

below is my code

public FileResult DownloadPDF(int id)
{
    var downloadFileName = id.ToString() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss")+".pdf";
    // TemporaryFileHelper implements the Idisposable
    using(TemporaryFileHelper tempDownloadFile = new TemporaryFileHelper())
    {
        //obtain temp file name and path 
        var tempFileNameAndPath = tempDownloadFile.GetTempFileNameWithPath();
        //generate temp file with above file name and path
        _pdfReportServices.GenerateTempPdfForDownload(id, tempFileNameAndPath);
        //return file result
        return File(tempFileNameAndPath, "application/pdf", Server.UrlEncode(downloadFileName));
    }

}
Tim
  • 1
  • Dipose is used to remove unmanaged resources after using it. So call your Dispose method after downloading your file. – Shakeer Hussain Jun 05 '17 at 15:17
  • No, do not close it. The File() method returns a FileResult , see the linked duplicates. – H H Jun 05 '17 at 18:02
  • Another [link](https://stackoverflow.com/q/42238826) – H H Jun 05 '17 at 18:06
  • Thanks for answering,however the file is saved as a temp file in app folder .I only pass a string of file path, but a filestream to File(). How did the file get disposed before the return? – Tim Jun 06 '17 at 13:47

0 Answers0