开发者

Using Jquery and Ajax to save a file in ASP.Net

开发者 https://www.devze.com 2023-01-04 10:52 出处:网络
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:

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",
          开发者_如何学Go  data: "id=0",
            success: function(){
            }
        });
    });
});


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消