Mvc ajax download return File( string






















My original answer below was over 3 years old, so I thought I would update as I no longer create files on the server when downloading files via AJAX however, I have left the original answer as it may be of some use still depending on your specific requirements. A common scenario in my MVC applications is reporting via a web page that has some user configured report parameters Date Ranges, Filters etc.

When the user has specified the parameters they post them to the server, the report is generated say for example an Excel file as output and then I store the resulting file as a byte array in the TempData bucket with a unique reference.

This reference is passed back as a Json Result to my AJAX function that subsequently redirects to separate controller action to extract the data from TempData and download to the end users browser. One other change that could easily be accommodated if required is to pass the MIME Type of the file as a third parameter so that the one Controller action could correctly serve a variety of output file formats.

ReadAllBytes file. Filepath ; return File fileBytes, System. Octet, file. Add a comment. Active Oldest Votes. Govinda Rajbhar Govinda Rajbhar 2, 3 3 gold badges 35 35 silver badges 59 59 bronze badges.

This example worked for me. This example sends an HttpGet request, and thus the data payload just one record ID in the request is included as parameters in the URL. But, what if I wanted to send an HttpPost request, with the data payload included as an object containing many fields in the request? Gedao Gedao 1, 9 9 silver badges 13 13 bronze badges. Is it easier for you to pass model id to the action and then find your student by id and do whatever you want with student model data.

I have updated the answer. Elvin Acevedo Jun Go to top. Layout: fixed fluid. Web Developer. Hong Kong. First Prev Next. Very helpful. Introduced me to 3 new concepts I did not know about: 1 separating the request for file creation from the further request to download the file; 2 BlockUI jQuery plugin; 3 ActionFitlerAttribute. This page will automatically be redirected to the sign-in page in 10 seconds.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience. We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.

Microsoft has ended support for older versions of IE. For the best experience, upgrade to the latest version of IE, or view this page in another browser. Unfortunately, activation email could not send to your email. Please try again. Claim your free account. Contact Us. Sign In. Developer Platform Overview. Source Code on Github. Speak your mind Please login to post your comment!

Toggle navigation. Home About Contact Register Log in. FileResult The action result that returns FileResults writes files as a response. Text; cmd.

Speak your mind. Please login to post your comment!



0コメント

  • 1000 / 1000