Click here to Skip to main content
14,689,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

From Jquery ajax i am making a C# function call which is returing a file download option.

If i am using $.ajax, it is not working, if i am giving window.location = url then that download box is coming.

I am trying to download one excel file.

I have given this content type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet in the c# response object and writing that C# response object property OutputStream write to memorystream object with the proper response header.

Below is the AJAx code which i am using to call the URL.
url: controllerUrl,
type: 'Get',                                                            contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                            beforeSend: function (xhr) {
                                    xhr.overrideMimeType('text/plain; charset=UTF-8')
                                    $(that.el).parent().block({ message: "Processing..." });
                                success: function (data) {
                                complete: function (data) {
                                error: function (jqXHR,textStatus) {

Please let me know if i missed anything. .
Updated 14-Mar-12 23:36pm
P.Salini 15-Mar-12 4:36am
added <pre> tags
Member 7217874 15-Mar-12 4:38am
thanks ...
Member 7217874 15-Mar-12 5:15am
.what i want to acheive is i have a dialog box appears when i click on OK button it is prompting me to download a file. But while server prepares the file to get downloaded i want that dialog box to be blocked and after file ready for download i want to close that dialog box. so that i made it that ajax request.

Pl let me know if there is any other solution...
BobJanova 15-Mar-12 9:38am
I'm not sure this is possible. I don't think you can get an event for 'the content pointed to by this link was completely downloaded', and you can only get the browser to download a file by a direct link, as far as I know. The only thing I can think of is if there is some way to make a javascript: url return something with headers and a Content-Type/Content-Disposition. But I don't think there is.

1 solution

You can not use AJAX to start download, browser will not show Save dialog.
You can use ajax to check if file is ready and if it is use javaScript
to add a hidden iframe to DOM and assign url to your file to src attribute.
That will start the download.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900