Click here to Skip to main content
15,846,571 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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 :
JavaScript
$('#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;
        }
    ); 


and in the server side the page load function of CreateTextFile.aspx:
C#
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)
            {
            }
        }
Posted
Updated 9-Jan-13 3:50am
v3
Comments
[no name] 9-Jan-13 13:59pm    
why dont you just write the file directly using the base stream from the response and the FileStream

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

JavaScript
$('#ActivateBtn').click
    (
        function () {
 
            var SocietyNameTBxvar = document.getElementById("<%=SocietyNameTBx.ClientID%>");
            var UserNbreTBxvar = document.getElementById("<%=UserNbreTBx.ClientID%>");
            var iframe = document.createElement("iframe");
iframe.src = encodeURI("CreateKeyFile.aspx?company=" + SocietyNameTBxvar +"&users=" + UserNbreTBxvar);
document.body.appendChild(iframe);
return false;
        }
    ); 

and it works like magic =)
 
Share this answer
 
Comments
shwetavagh 6-May-13 3:10am    
Thank you
Brahmadath 5-Jan-15 2:18am    
Aweeeeeeesome .. Thanks ... :)
Hello!
What if I want to send modified headers when requesting the file (authentication ticket)?
 
Share this answer
 
Wow. Just wow.

What do you think AJAX is ? It's a way to get data back to the client without a refresh . You can't download a file without a full page request, but if your request sends back a file, the page won't change, it will just ask the user to save. If you do an AJAX request, you can't do a Response.Redirect. You're not handling a full page request to start with. You need to get rid of the redirect ( which is also invalid if you're sending a file as the response ) and make this not an AJAX call.
 
Share this answer
 
Comments
GLolita 8-Jan-13 5:46am    
okay, so I removed the redirection statement, now on the success function what should i do? I'm sorry if I look like a bit lost, I kinda biginner in ajax developement.
Christian Graus 8-Jan-13 14:09pm    
YOU CAN'T DOWNLOAD A FILE WITH AJAX !!! Just because something is a cool buzzword, does not mean it's always the best, or even a viable choice.
Ruchin Munjal 22-Jan-13 16:05pm    
Christian,
Wow. Just wow.
You started your answer by ridiculing him and expect him to read your answer carefully. He replied back saying he's new in AJAX development. I can see you are a programming genius but some etiquette can go a long way. and who said anything about money??? Wow. Just wow.
Christian Graus 22-Jan-13 16:18pm    
Thanks for joining us. Yes, if I take the time to do someone's job for them, I hope they read my answer. Sorry about that.
Ruchin Munjal 22-Jan-13 16:37pm    
I agree with you that he should have read it twice may be if it wasn't clear, but i kind of expected this because of the way you started. Anyways, even I was a bit harsh and I'm sorry too. Peace!! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900