Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to make the following solution to work:
User comes to the aspx page and inserts dokument ID in textbox. Then this ID is used to generate txt file from database. User should be prompted to download the file.

I have implemented this with simple postback, but when I tried to use ajax with WebMethod it's not popping up user to download the file. In other words - the following is no longer working:

C#
HttpContext.Current.Response.ContentType = "txt/application";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Txt_document_title.txt");
            HttpContext.Current.Response.OutputStream.Write(Txt_Stream.GetBuffer(), 0, Txt_Stream.GetBuffer().Length);


I read on the documentation page that it wont work with WebMethod, but not really sure what would be the best practice here. Also - the file should not be saved anywhere due to security reasons and should go directly to user.

Thanks!
Posted
Comments
Arkadeep De 15-Mar-15 4:36am    
whenever you are downloading something from the site the page should be posting back. what ever you have tried previously tried that within UpdatePanel with the download button in trigger.
JackTheGreatest 15-Mar-15 5:30am    
But then how do I know, from the client perspective, if my request is still loading?
For example I press the button and it takes ~10 seconds to generate the document and the user would be waiting and staring to the screen. I am planning to add some clientside event to show a spinner-wheel indicating that the document is being loaded so I cannot do postback here.
Modi Mayank 15-Mar-15 11:07am    
For that you can use AJAX progress bar along with update panel. Here is the simple example for simple AJAX example of update panel with progressbar
Arkadeep De 15-Mar-15 12:08pm    
I will go with Modi. You can easily use AJAX progress bar...

1 solution

The best approach is to redirect user to another page, where you will start the download in the page load itself and on that page you will display "Thanks for downloading" or something like that.
 
Share this answer
 
Comments
JackTheGreatest 15-Mar-15 5:35am    
How to do this from a WebMethod (you can not redirect from webmethod)? And how will that solve the current situation when I am not going to store(create) file and need it to be created and pushed to user without it being saved anywhere.
No need to execute WebMethod. Just use normal server side method or an event and inside that redirect to a new download page. On the page load of that page, get the required data from database and then assign that to the response object.
Arkadeep De 15-Mar-15 12:08pm    
or you can use windows.location.href = "your page url";

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