0

I am working on a client-server application, and at the moment i am returning a file result in my web api controller, like this:

 FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes(docDestination), "application/msword")
            {
                FileDownloadName = "myFile.docx"
            };
            return result;

on my client side i receive the response like this:

enter image description here

i thaught at begin that the browser detects the file automaticly and fires the download dialog, but no, so i tried to treat the result as a blob like this:

   .then(response => {
                console.log("here lives the response:", response);
                var headers = response.headers;
                var blob = new Blob([response.bodyText], { type: headers['application/msword'] });
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "Filename";
                link.click();

the download fires, but the content is not identified as docx file, and his content is broken.

Any tip on how to do it?

Thanks

Filipe Costa
  • 465
  • 1
  • 6
  • 26
  • The first two answers here look promising https://stackoverflow.com/questions/1066452/easiest-way-to-open-a-download-window-without-navigating-away-from-the-page – Reiner Aug 12 '17 at 16:51
  • tried both, in one of them i get a error transfering the file – Filipe Costa Aug 12 '17 at 16:56
  • can't i do nothing with my response? – Filipe Costa Aug 12 '17 at 16:56
  • Here is almost the same asked and solved https://stackoverflow.com/questions/24080018/download-file-from-an-asp-net-web-api-method-using-angularjs and even a plugin FileSaver.js recommended. – Reiner Aug 12 '17 at 18:28

0 Answers0