Asp mvc routing file download link






















NOTE: This example code above fails to properly account for international characters in the filename. See RFC for the relevant standardization. I believe recent versions of ASP. So I'm posting what worked for me as an alternative in case anybody else is having trouble. Darin Dimitrov's answer is correct. Just an addition:. AppendHeader "Content-Disposition", cd. ToString ; may cause the browser to fail rendering the file if your response already contains a "Content-Disposition" header.

In that case, you may want to use:. Below code worked for me for getting a pdf file from an API service and response it out to the browser - hope it helps;. Action method needs to return FileResult with either a stream, byte[], or virtual path of the file. You will also need to know the content-type of the file being downloaded. Sample video link How to download files using asp.

If, like me, you've come to this topic via Razor components as you're learning Blazor, then you'll find you need to think a little more outside of the box to solve this problem. It's a bit of a minefield if also like me Blazor is your first forray into the MVC-type world, as the documentation isn't as helpful for such 'menial' tasks. Next, make sure your application startup Startup. Hopefully this helps anyone who struggled like me!

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 7 months ago. Active 1 year, 7 months ago. Viewed k times. These are the examples of what I've tried so far. Data, document. ContentType, document.

Data , document. Add HeaderNames. ContentDisposition, cd. ToString ; return File document. Nick Albrecht. Nick Albrecht Nick Albrecht 16k 9 9 gold badges 67 67 silver badges 97 97 bronze badges. Add a comment. Active Oldest Votes. Oskar Berggren 5, 1 1 gold badge 15 15 silver badges 35 35 bronze badges.

Darin Dimitrov Darin Dimitrov k gold badges silver badges bronze badges. If I recall correctly, it can be un-quoted so long as the filename has no spaces I ran mine through HttpUtility.

UrlEncode to achieve this. It will work in IE, but Chrome will report a duplicate header and refuse to present the image. Using MVC 5, there is no need for the content-disposition header anymore, as it is already part of the response header. Show 8 more comments. The document variable was just a class POCO representing the information about the document you wanted to return. That was asked on the answer accepted as well. It could come from an ORM, from a manually built SQL query, from the file system as yours pulls information from , or some other data store.

Thanks for contributing an example using just the file system though. Thanks, Saved my day : — Dipesh. An alternative to AppDomain. BaseDirectory is System. Do i have to use javascript in order to start the download? Connect and share knowledge within a single location that is structured and easy to search. I am testing Doddle Report to generate a few report form a IEnumerable object.

I need to generate a Link like this. Check out this tutorial to return different action result. This approach sounds to be the best while using MVC. PS: I've forced a. You should do what is intersting for you to get the specific file.

Create a LinkButton on your form with a handler that generates the PDF file, then redirect to the created file. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to Generate a Link to download a File in asp. Ask Question. Asked 12 years, 7 months ago. Active 6 years, 5 months ago. Viewed 30k times. Improve this question.

Jedi Master Spooky. Jedi Master Spooky Jedi Master Spooky 5, 11 11 gold badges 50 50 silver badges 85 85 bronze badges. What about this link that you want to generate? I add a test. Another way, if you want to click the button and download immediately without viewing, the code should be like this:. Sorry i was incomplete in my description but i want to be able to supply full filename on the url. The content you requested has been removed. Ask a question.

Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums.



0コメント

  • 1000 / 1000