使用ajax post方法从asp按钮下载文件 [英] download file from an asp button using ajax post methods

查看:102
本文介绍了使用ajax post方法从asp按钮下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下载一个包含当前页面数据的文本文件,点击一下asp:按钮。

所以我在我当前的asp页面上用ajax方法发送所需的数据作为ajax params到另一个aspx页面,负责创建文本文件,

so:

I need to download a text file containing data from my current page, this by a click on an asp:button.
so I used on my current asp page an ajax method to send the required data as ajax params to another aspx page which is responsible for the creation of the text file,
so :

$('#ActivateBtn').click
    (
        function () {

            var SocietyNameTBxvar = document.getElementById("<%=SocietyNameTBx.ClientID%>");
            var UserNbreTBxvar = document.getElementById("<%=UserNbreTBx.ClientID%>");
            
            var params = "{ company : \"" + SocietyNameTBxvar.value + "\", users : \"" + UserNbreTBxvar.value +"\"}";
                
            $.ajax (
            {
                type: "POST",
                url: "CreateTextFile.aspx",
                data: params,
                contentType: "application/x-www-form-urlencoded",
                async: true,
                cache: false,
                success: function (result) {
                // don't know how to get the text file !
                },
                error: function (result) {
                alert("Due to unexpected errors we were unable to load data");
            }
                }

            );
            return false;
        }
    ); 





和服务器端CreateTextFile.aspx的页面加载功能:



and in the server side the page load function of CreateTextFile.aspx:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HttpResponse Response = HttpContext.Current.Response;

                string requestType = Request.Params["requestType"];


                //populate variables from posted data
                string company = Request.Params["company"];
                string users = Request.Params["users"];
                string attachment = "attachment; filename=" + filename;

                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", attachment);
                Response.ContentType = "application/octet-stream";
                using (StreamWriter LicFile = new StreamWriter(Response.OutputStream, Encoding.UTF8))
                {
                    LicFile.WriteLine(Resx.SocietyName + " : " + company);
                    LicFile.WriteLine(Resx.UserNbre + " : " + users);
                    
                }
                Response.End();
            }
            catch (Exception exp)
            {
            }
        }

推荐答案

' #ActivateBtn' ).click

function (){

var SocietyNameTBxvar = document .getElementById( < %= SocietyNameTBx.ClientID%>);
var UserNbreTBxvar = document .getElementById( <%= UserNbreTBx.ClientID%>);

var params = {company: \ + SocietyNameTBxvar.value + \,用户:\ + UserNbreTBxvar.value + \};
('#ActivateBtn').click ( function () { var SocietyNameTBxvar = document.getElementById("<%=SocietyNameTBx.ClientID%>"); var UserNbreTBxvar = document.getElementById("<%=UserNbreTBx.ClientID%>"); var params = "{ company : \"" + SocietyNameTBxvar.value + "\", users : \"" + UserNbreTBxvar.value +"\"}";


.ajax(
{
type: POST
url: CreateTextFile.aspx
data:params,
contentType: application / x-www-form-urlencoded
async:< span class =code-keyword> true ,
cache: false
成功:功能(结果){
// 不知道如何获取文本文件!
},
错误: function (结果){
alert( 由于意外错误,我们无法加载数据);
}
}

);
return false ;
}
);
.ajax ( { type: "POST", url: "CreateTextFile.aspx", data: params, contentType: "application/x-www-form-urlencoded", async: true, cache: false, success: function (result) { // don't know how to get the text file ! }, error: function (result) { alert("Due to unexpected errors we were unable to load data"); } } ); return false; } );





和服务器端CreateTextFile.aspx的页面加载功能:



and in the server side the page load function of CreateTextFile.aspx:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HttpResponse Response = HttpContext.Current.Response;

                string requestType = Request.Params["requestType"];


                //populate variables from posted data
                string company = Request.Params["company"];
                string users = Request.Params["users"];
                string attachment = "attachment; filename=" + filename;

                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", attachment);
                Response.ContentType = "application/octet-stream";
                using (StreamWriter LicFile = new StreamWriter(Response.OutputStream, Encoding.UTF8))
                {
                    LicFile.WriteLine(Resx.SocietyName + " : " + company);
                    LicFile.WriteLine(Resx.UserNbre + " : " + users);
                    
                }
                Response.End();
            }
            catch (Exception exp)
            {
            }
        }


我不需要制作一个完整的ajax帖子,我只需要创建一个能让我得到回复的iFrame



I don't need to make a whole ajax post , I just need to create an iFrame that will enable me to get the response


这篇关于使用ajax post方法从asp按钮下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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