Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 :
$('#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:
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 7-Jan-13 6:51am
GLolita484
Edited 9-Jan-13 3:50am
v3
Comments
Mathlab at 9-Jan-13 13:59pm
   
why dont you just write the file directly using the base stream from the response and the FileStream
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
GLolita at 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 at 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 at 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 at 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 at 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!! :)
GLolita at 9-Jan-13 5:07am
   
I just can not believe how rude you are ! we're not to blame each other for our mistakes but to correct them together, you know! pardon my ignorance but i thought this is why i'm here, to find someone helping me with that !
Christian Graus at 9-Jan-13 5:08am
   
I am sorry. I told you, and you didn't understand me. So I tried again. Is it rude to try to help people who really don't know enough to be programming for money, as you clearly appear to be ?
thewazz at 24-Oct-13 0:14am
   
it's why i stopped asking questions here.
Christian Graus at 24-Oct-13 1:42am
   
Yeah, it's largely why I stopped answering, too. People are not interested in help, or direction, they mostly want their work done for them, and get upset when asked to understand something or think or write code.
GLolita at 9-Jan-13 6:00am
   
thank you for your effort anyway,PS: I'm still a trainee !
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
$('#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 =)
  Permalink  
Comments
shwetavagh at 6-May-13 3:10am
   
Thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello!
What if I want to send modified headers when requesting the file (authentication ticket)?
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 11 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100