Just a couple of days ago, I found myself pondering the same question. The task at hand involved a project utilizing ExtJS on the client side and ASP.Net on the server side. My mission was to convert the server-side functionality to Java. One particular challenge was the need to download an XML file generated by the server in response to an Ajax request from the client. Typically, downloading a file after an Ajax request is not feasible as it needs to be stored in memory first. However, in the original application, the browser displayed a standard dialog with options to open, save, or cancel the download - a behavior seemingly unique to ASP.Net that took me two days to confirm as I explored alternative methods to achieve the same result.
public static void WriteFileToResponse(byte[] fileData, string fileName)
{
var response = HttpContext.Current.Response;
var returnFilename = Path.GetFileName(fileName);
var headerValue = String.Format("attachment; filename={0}",
HttpUtility.UrlPathEncode(
String.IsNullOrEmpty(returnFilename)
? "attachment" : returnFilename));
response.AddHeader("content-disposition", headerValue);
response.ContentType = "application/octet-stream";
response.AddHeader("Pragma", "public");
var utf8 = Encoding.UTF8;
response.Charset = utf8.HeaderName;
response.ContentEncoding = utf8;
response.Flush();
response.BinaryWrite(fileData);
response.Flush();
response.Close();
}
This method was invoked from a WebMethod, which in turn was triggered by an ExtJS.Ajax.request - truly magical. In my case, I ultimately resorted to using a servlet and a hidden iframe to achieve similar functionality...