6

I have a button that uses jQuery and ajax to call a server side script to create a text file and sends back the following response:

Response.ContentType = "csv";
Response.AddHeader("Content-disposition", "attachment; filename=" + fName);
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(btFile);
Response.End();

However, the save dialog does not appear. If I don't use ajax and perform a full postback with the same code, it works. Any ideas?

Here is the jQuery code:

$(function() {
    $('#reportButton').click(function() {
        $.ajax({
            type: "POST",
            url: "GenerateReport.aspx",
            data: "id=0",
            success: function(){
            }
        });
    });
});

2 Answers 2

3

Rather than using AJAX (which will not work, as Brian mentions), you can fake it by using jQuery to dynamically create a form and an iframe to post it to. Here is an example I found -- you should read through the comments for some improvements (like the use of a dynamically created iframe to prevent problems if your page does not return the proper headers).

Sign up to request clarification or add additional context in comments.

Comments

1

I think the issue is the AJAX, and if the request was made as a standard request outside JQuery, you would get the save dialog box. JQuery requests would stream the data to the callback...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.