Mvc web api download file






















Introduction File downloading is a kind of common functionality which we want in our web application. Copy Code. Contact Me! User Sep How do you know so much about swallows? Well, you have to know these things when you're a king, you know. VICK Sep Tridip Bhattacharjee Sep John Brett Sep Go to top. Layout: fixed fluid. Software Developer. CreateResponse HttpStatusCode. OK ; result. Open, FileAccess. Read ; result. ContentDispositionHeaderValue "attachment" ; result.

Improve this question. Scott What would be the filetype? RashminJaviya Could be. Net framework you are using? Kurkula you should use File of System. File not from controller — Javysk. Show 1 more comment. Active Oldest Votes. Simple download method: You can have the browser download the requested file simply by using the code below, and this is supported in all browsers, and will obviously trigger the WebApi request just the same.

URL window. I hope this helps. Improve this answer. Scott Scott Hi Scott I used your method and it works but the browser save the file as type html not pdf. Do you know what could I do wrong? Thanks Kiran, However I had already implemented a very similar approach without async. I will accept your answer as I believe that this is the right answer for the given simple scenario. Kiran Challa, please do you think you could help me with this question : stackoverflow.

KiranChalla I found this hanselman. Add new MethodOverrideHandler ; the class isn't recognized. I also tried to download it from NuGet, but I couldn't because I'm using netframework v 4. Show 1 more comment. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I've had it suggested to me that I should use FileResult to allow users to download files from my Asp.

Net MVC application. But what if I can't know the file type? I want users to be able to download pretty much any file from the filearea of my site. EDIT: I got the answer below , but just thought I should post the full working code if someone else is interested:. The MVC framework supports this natively. The System. If you're using. NET Framework 4. This is how I've used it in my action. You only need to specify the virtual path of the file and the name to be saved as.

In my example i was storing the physical path of the files so i used this helper method -that i found somewhere i can't remember- to convert it to a virtual path. GetFile should be closing the file or opening it within a using. Then you can delete the file after conversion to bytes-- the download will be done on that byte buffer.

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. Download file of any type in Asp. Ask Question. Asked 11 years, 2 months ago. There are many ways to implement the hyperlink. For example, we can create a blob data URL, use a third-party JavaScript library, or serve files directly from a web server.

In this article, we are going to use the simplest way to create an anchor element without any JavaScript. The code is just one line, as shown in the following snippet. The href attribute has a value that points to the API endpoint. Another attribute in the anchor element is the download attribute, which prompts the user to save the linked URL instead of navigating to it.

You can read more about the download attribute here. The rendered hyperlink works in all major browsers like Chrome, Edge, and Firefox. It works like the screen recording below. Everything works as expected.



0コメント

  • 1000 / 1000