4

i am trying to download a file (.docx) from asp.net web api.

Since i already have a document in the server i set the path to existing one and then i follow something sugested on stackoverflow and do this:

docDestination is my path.

 HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(docDestination, FileMode.Open, FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); return result; 

after that on my client side i try to do this:

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

this is what i get on my response

response

what i get:

what i get

any help?

3 Answers 3

8

Just add ContentDisposition to your response header with value of attachment and the browser will interpret it as a file that needs to be download

HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(docDestination, FileMode.Open,FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "document.docx" }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); return result; 

Take a look in this link for more information in ContentDisposition header

Sign up to request clarification or add additional context in comments.

Comments

7

Change return type of your method. You can write method something like this.

public FileResult TestDownload() { FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes("YOUR PATH TO DOC"), "application/msword") { FileDownloadName = "myFile.docx" }; return result; } 

In client side, you just need to have a link button. Once you click on the button, file will be downloaded. Just write this line in cshtml file. replace controller name with your controller name.

@Html.ActionLink("Button 1", "TestDownload", "YourCOntroller") 

5 Comments

how can i get it on the client side?
@FilipeCosta, updated my answer. Please have a look.
i am not using razor syntax
@FilipeCosta, what are you using?, just javascript?
I think you can call same API multiple time. You will be able to download multiple files. Let me know if you need help in that
2

When you have a stream open, you want to return it's content as a file

[HttpGet] public async Task<FileStreamResult> Stream() { var stream = new MemoryStream(System.IO.File.ReadAllBytes("physical path of file")); var response = File(stream, "Mime Type of file"); return response; } 

You use it when you have a byte array you would like to return as a file

[HttpGet] public async Task<FileContentResult> Content() { var result = new FileContentResult(System.IO.File.ReadAllBytes("physical path of file"), "Mime Type of file") { FileDownloadName = "Your FileName" }; return result; } 

when you have a file on disk and would like to return it's content (you give a path)-------------only in asp.net core

[HttpGet] public async Task<IActionResult> PhysicalPath() { var result = new PhysicalFileResult("physical path of file", "Mime Type of file") { FileDownloadName = "Your FileName", FileName = "physical path of file" }; return result; } 

5 Comments

Why are you try/catching everything? There's no need for that to demonstrate your answer.
Why are you passing string values to your HttpGet attribute? This wouldn't compile.
How do you know this is ASP.NET Core? The question isn't tagged as ASP.NET Core.
How about PhysicalFileResult? Isn't that only in ASP.NET Core? Please put some thought into your answer before throwing random code out there.
So, why answer if you don't know about the subject matter being asked about? There is no requirement for you to answer. In fact, it's better that you don't if you aren't familiar with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.