Length ;. Return Convert. End Function. When any of the three Button is clicked, the DownloadFile JavaScript function is called and the name of the File is passed to it. URL window. Config Setting. You will need to set the maxJsonLength property through Web. Config configuration using the system.
This is required as the size of the JSON response exceeds the default value when Files in Base64 string format are downloaded. Note : For more details, please refer ASP. Browser Compatibility. Related Articles. Add Comments. Thank you for the feedback. The comment is now awaiting moderation. You will be notified via email when the author replies to your comment.
Please select a comment to reply. When processing the response, you inject an iframe in your body and set the iframe 's SRC to the URL you just received like this using jQuery for the ease of this example :. If you've set the correct headers as shown above, the iframe will force a download dialog without navigating the browser away from the current page. After you've received the JSON response, you can then decide client-side what to do with it. Maybe, for example, later on you want the user to click a download link to the URL instead of forcing the download directly, in your current setup you would have to update both client and server-side to do so.
I see you've already found out a solution, however I just wanted to add some information which may help someone trying to achieve the same thing with big POST requests. I had the same issue a couple of weeks ago, indeed it isn't possible to achieve a "clean" download through AJAX, the Filament Group created a jQuery plugin which works exactly how you've already found out, it is called jQuery File Download however there is a downside to this technique.
In slow Internet connections you'll have to wait a lot until the request is sent and also wait for the file to download. But as I discovered in my own app, for bigger file sizes it is almost unbearable. My app allow users to export images dynamically generated, these images are sent through POST requests in base64 format to the server it is the only possible way , then processed and sent back to users in form of.
In slow Internet connections it can be really annoying. My solution for this was to temporary write the file to the server, once it is ready, dynamically generate a link to the file in form of a button which changes between "Please wait This makes all the waiting time more bearable for users, and also speed things up. Months have passed since I posted this, finally I've found a better approach to speed things up when working with big base64 strings. I now store base64 strings into the database using longtext or longblog fields , then I pass its record ID through the jQuery File Download, finally on the download script file I query the database using this ID to pull the base64 string and pass it through the download function.
I know this is way beyond what the OP asked, however I felt it would be good to update my answer with my findings. For those looking a more modern approach, you can use the fetch API. The following example shows how to download a spreadsheet file. It is easily done with the following code. Also, it has a similar syntax to the jQuery approach, without the need to add any additional libraries. Of course, I would advise checking to which browser you are developing, since this new approach won't work on IE.
You can find the full browser compatibility list on the following [link][1]. This url must be set, on my example I am assuming you know this part. Also, consider the headers needed for your request to work.
I want to point out some difficulties that arise when using the technique in the accepted answer, i. You can't set headers on the request. If your authentication schema involves headers, a Json-Web-Token passed in the Authorization header, you'll have to find other way to send it, for example as a query parameter. You can't really tell when the request has finished. Well, you can use a cookie that gets set on response, as done by jquery.
It won't work for concurrent requests and it will break if a response never arrives. You can only use the content types supported by a form.
Which means you can't use JSON. I ended up using the method of saving the file on S3 and sending a pre-signed URL to get the file. As others have stated, you can create and submit a form to download via a POST request. However, you don't have to do this manually. One really simple library for doing exactly this is jquery. It provides an API similar to the standard jQuery. This is a 3 years old question but I had the same problem today.
I looked your edited solution but I think that it can sacrifice the performance because it has to make a double request.
So if anyone needs another solution that doesn't imply to call the service twice then this is the way I did it:. This form is just used to call the service and avoid to use a window. After that you just simply have to make a form submit from jquery in order to call the service and get the file. It's pretty simple but this way you can make a download using a POST. I now that this could be easier if the service you're calling is a GET , but that's not my case.
I used this FileSaver. In my case with csv files, i did this in coffescript :. I think for most complicated case, the data must be processed properly. Under the hood FileSaver. To get Jonathan Amends answer to work in Edge I made the following changes:. Below is my solution for downloading multiple files depending on some list which consists of some ids and looking up in database, files will be determined and ready for download - if those exist.
And Yes, like others said, it is possible to do it in jQuery Ajax. I did it with Ajax success and I am always sending response As long as you return response , success in Ajax can work with it, you can check if file actually exist or not as the line below in this case would be false and you can inform user about that:.
But I am referring to a page that must first be processed and then downloaded. I know browsers block multiple file downloads, and I also have API which returns a set of csv formatted data. If anyone can help me improve this that would be great, but it's working for me so far. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Add Comment. Disclaimer : The code samples and API available at www.
You are free to use it for commercial as well as non-commercial use at your own risk, but you cannot use it for posting on blogs or other tutorial websites similar to www.
All the code samples and API provided by the authors are solely their creation and neither the author nor the site are responsible if it does not work as intended. I agree to the above terms. Download Required. What our readers say. Error Details. This site makes use of Cookies. Please refer Privacy Policy for more details.
Got it.
0コメント