views:

65

answers:

2

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(){

            }

        });

    });

});
+1  A: 

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...

Brian
+1  A: 

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).

patmortech