Download file using ajax in mvc






















Sign in Email. Forgot your password? Ask a Question. Please Sign up or sign in to vote. See more: C. I'm trying to implement file download functionality thru ajax call in MVC. After calling of controller method i always have a "parseerror", can somebody explain me why?

What am I doing wrong here? What I have tried:. Copy Code. Posted 6-Aug am Member Add a Solution. How to avoid that? There is absolutely no need to use ajax for this at all.

Just call window. All this is doing is making the same request twice. I have no idea why this was upvoted. Show 4 more comments. Equals Constants. Combine Path. Message ; return Content e. GetExtension path. Raw Localizer["Report has been exported successfully"]. ReadAllBytes file1. FullName ; return File fileBytes, System. Dilshad Dilshad 1 1 1 bronze badge. A big block of code thrown up on the web without explanation doesn't help anyone.

You should explain your approach and why your answer is better or different from the other answers. Copy Code. Member Jul Majid Qafouri Jan Ghouse M 1-Jan MarceloGuy Sep Elvin Acevedo Jun Go to top. Layout: fixed fluid. Web Developer. Hong Kong. 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.

This removes any need for any physical files to created and stored on the server, so no housekeeping routines required and once again this is seamless to the end user.

Note, the advantage of using TempData rather than Session is that once TempData is read the data is cleared so it will be more efficient in terms of memory usage if you have a high volume of file requests. See TempData Best Practice.



0コメント

  • 1000 / 1000