1

I want the user to be able to download .docx documents from a website. The file exists and path is correct because file exists is returning true, but in the view, browser says FileNotFoundException.

If I copy the error path and past into explorer address box, it opens the document.

public IActionResult DownloadDocument(int docID) 
{
    if (System.IO.File.Exists(@"C:\Users\Folder1\source\repos\Folder2\Folder3\Contents\Folder4\CustomerFeedback.docx"))
         return File(@"C:\Users\Folder1\source\repos\Folder2\Folder3\Contents\Folder4\CustomerFeedback.docx", "application/docx", "CustomerFeedback.docx");
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
AliAzra
  • 889
  • 1
  • 9
  • 28
  • Try doing Directory.GetFiles(@"C:\Users\Folder1\source\repos\Folder2\Folder3\Contents\Folder4") – Qusai Azzam Apr 21 '19 at 13:32
  • Where did you get that `application/docx` was a valid MIME type? https://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc – ardila Apr 21 '19 at 13:40
  • Hi Ardila. I get that code from https://www.c-sharpcorner.com/UploadFile/rahul4_saxena/download-file-in-mvc-4/ – AliAzra Apr 21 '19 at 13:41

1 Answers1

2

Try using the proper MIME type for DOCX - application/docx will not be served up by IIS as it's not a registered content type.

Content Type for DOCX: application/vnd.openxmlformats-officedocument.wordprocessingml.document

Reference: https://stackoverflow.com/a/4212908/175679

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • Thank you Silver Ninja I updated and it doesn't give me error now but the download didn't start. It is just opening the view page. I thought this code starts a file download. – AliAzra Apr 21 '19 at 13:57