Jquery - request for excel file on $ .get

Internet explorer is used to request the user to download the excel file after completing Response.Write

Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment;filename=\"sheet.xls\""); Response.RedirectLocation = "export.xls"; Response.Charset = ""; EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); dataGridResult.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); 

This works when I send a POST to a page with a button click event.

I use the page as a service and do $.get() , but the results are sent back as HTML. I am not prompted to open the excel file. How to send a request to a user?

 $.get('ExcelService.aspx', { batches: input }, function (data) { alert(data);//I see HTML }); 
+4
source share
2 answers

This is a similar thread with a similar problem ("I would like to make an async GET request that will return a document with the MIME content type and make it display the browser’s Save dialog.")

How to specify content type and content location with $ .ajax () GET response

Someone out there offers a workaround:

If you want to open the save dialog box programmatically, you can use jQuery to add a hidden iframe to the page with an url in the form of src. If necessary, place a dialog box.


SAMPLE
jquery - on click (no ajax / get needed)
  var dynamicUrl = 'ExcelService.aspx?batches=' + input; $('#excelPopup').attr('src', dynamicUrl); window.frames["#excelPopup"].location.reload(); 

HTML

 <iframe id="excelPopup" style="visibility:hidden;height:0px;width:0px;"></iframe> 
+2
source

You can try Ajax post

 $.ajax({ dataType: "HTML", cache: false, type: "GET", url: 'ExcelService.aspx'; 

this will tell get and has a little more flexibility than just using jquery

0
source

All Articles