使用JQuery和Ajax保存在ASP.Net文件 [英] Using Jquery and Ajax to save a file in ASP.Net

查看:93
本文介绍了使用JQuery和Ajax保存在ASP.Net文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,使用jQuery和AJAX调用服务器端脚本来创建一个文本文件, 发送回下面的响应

  Response.ContentType =CSV;

        Response.AddHeader(内容处置,附件;文件名=+ FNAME);

        Response.ContentType =应用程序/八位字节流;

        Response.BinaryWrite(btFile);

        到Response.End();
 

但保存对话框不会出现。 如果我不使用AJAX和相同的code它的工作原理进行完全回发。任何想法?

下面是jQuery的code

  $(函数(){

    $('#reportButton)。点击(函数(){

        $阿贾克斯({

            键入:POST,

            网址:GenerateReport.aspx

            数据:ID = 0,

            成功:函数(){

            }

        });

    });

});
 

解决方案

而不是使用AJAX(这是行不通的,因为布赖恩提到),你可以使用jQuery来动态创建一个表单和一个iframe将它张贴假的至。 下面是一个例子,我发现 - 你应该通过一些改进的意见看(如使用动态创建的iframe,以prevent问题,如果你的页面不返回正确的头)。

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

            }

        });

    });

});

解决方案

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

这篇关于使用JQuery和Ajax保存在ASP.Net文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆