tags:

views:

94

answers:

3

I have a link button on my page clicking on which, I download a file from some DMS system and then send the file after zipping it on server to the client using response.write.

But since the page is ajaxified, it throws an error.

Is possible to send a file to the client on a Ajax call?

I am using Telerik RadAjax.

A: 

You can send whatever data you like, you just have to be able to handle it in your JavaScript.

There isn't a great deal that JavaScript could do with a zipped file (unless you fancy finding, or writing, a zip decompression library in JavaScript).

David Dorward
I am not zipping the file on JS. M using another tool for doing it.
Ankit
I never said you were. You are sending a zipped file as the response. If you want JS to do anything useful with it you will have to unzip it in JS.
David Dorward
For that i need to download the file to client machine which is very large. So i want to compress it and then send it to client. Further, the problem is not with zipping, it is while sending the file using response.write method on ajaxified page.
Ankit
As mentioned. You can send the file to the client. The question is — what do you want the client to do with it? If you send it using Ajax, then the file can only be processed with JavaScript, and I can't think of anything that can usefully be done there without unzipping it.
David Dorward
David, the problem is not zipping / unzipping the file
Ankit
What is the problem then? As I've said several times, you **can** send whatever data you like to the client. I've also asked what you want the client to do with it, and you haven't answered that.
David Dorward
+1  A: 

There is no reason to request the file with an AJAX callback, as downloading the file doesn't refresh the page and therefore the user doesn't lose the context, which is usually the reason why you would prefer AJAX callback.

According to your comments, there are 2 ways to overcome the problem :

  1. You can write into the response stream at the same time that you are downloading the file from the second server and therefore making the progress visible in the open/save dialog of the browser.
  2. You can temporarily store the file somewhere in the database / file system and send it with a second request made directly by the user.

The first one seems more reasonable to me as you don't have to deal with the intermediate storage.

Thomas Wanner
I have a rad grid on my page which have link buttons. On click of any link button, i need to get the file from another server to the application server and then download the file to client. Since there might be delays in downloading file, i need to notify the user that file is getting downloaded. Thus i am using rad ajax manager to show a downloading panel till the file is downloaded.
Ankit
There is always a delay when downloading file, but each browser has a dialog for that which works in a standard and by the users expected way (e.g. all the downloads at one tab in Chrome or single popup with downloads in Safari/Firefox etc.). Why would you want to reimplement such a behavior in a different way ?
Thomas Wanner
The open/save file dialog box shows the download progress from app server to client machine. The time consuming task in my application is retreiving file from an external server to the application server and then sending it to client
Ankit
I see, then I would probably divide this functionality, make a request just to retrieve the file from the external server which shows the progress and notifies about success when it's done and then let the user simply click on the link when it's done and download the file using the standard mechanism.
Thomas Wanner
The third server is not visible to the end users. So i need to download the file on one go.
Ankit
Of course the server is not visible, otherwise the users could download the file directly from there. See answer edit ;)
Thomas Wanner
A: 

Don't use Response.Write or Response.WriteFile to force file-download because that will simply not help in this context.

In order to do what you want, save the zipped file on disk and redirect the user to download-file. You can create a temp folder to hold the zipped files which you create on the fly and flush them every one hour or any such predefined time-interval. You need to call this from standard post-back driven non-ajaxed call. This will preserve the state.

Response.Redirect("path-file-to-download");
Response.End();
this. __curious_geek
I cannot redirect my page .. as its a search page and i need to keep the user on same page.
Ankit
this will keep the user on same page. Browser will detect the file as downloadable non-html file. :)
this. __curious_geek