How to allow download of a file that is returned as binary data from AJAX

My problem is that I send the first PDF file to the client for download, then I need to check if there is any data in my database, then depending on this check I need to show that if the user wants to download another PDF file, I'm creating.

My code is:

//Here I just make dialog for question $('#printDWInfo').dialog({ resizable: false, modal: true, autoOpen: false }); //Here is my problem :) $('#generujWydruk').click(function (event) { event.preventDefault(); $('#printForm').submit(); // <-- sending first request and client get first PFD file $.post('<%: ResolveUrl("~/Reports/KPiRReportDWCheck") %>', <-- check for another data $("#printForm").serialize(), function(data) { if (data.length > 0) { $("#printDWInfo").dialog( "option", "buttons", [ { text: "Tak", click: function () { $.ajax({ type: "POST", url: '<%= Url.Action("PrintDWList","Reports")%>', datatype: "json", traditional: true, data:{'ids': data }, success: function (data2) { //I don't know what to do here } }); $(this).dialog("close"); } }, { text: "Nie", click: function () { $(this).dialog("close"); } } ]); $('#printDWInfo').dialog("open"); } } ); 

If the client clicks the "Tak" button in the dialog box, I use the ajax request because I can go to the array of int controllers that returns $.post('<%: ResolveUrl("~/Reports/KPiRReportDWCheck") %>' . In the successful function of my request, ajax FireBug shows me that data2 is the binary data of my PDF file, what do I need to do so that the client can download this PDF file?

+4
source share
2 answers

In the successful function of my ajax FireBug request I will show me that data2 is the binary data of my PDF file, what do I need to do so that the client can download this file in PDF format?

You should not use AJAX to upload files. The problem is that you are extracting pdf bytes in the javascript variable in the callback of your AJAX call, but nothing came of it. You cannot ask the user to save it, and, of course, you cannot save it to the client, since javascript does not have the necessary privileges.

So you should use a regular query:

 var downloadUrl = '<%= Url.Action("PrintDWList", "Reports")%>?' + $.param({ ids: data }, true); window.location.href = downloadUrl; 

Note that this will send the GET request to the PrintDWList action by passing the parameter of the identifier request string, so make sure that this action is available in the GET. Now, if the controller action uses the Content-Disposition header for attachment, it will prompt the user to download the file:

 public ActionResult PrintDWList(int[] ids) { byte[] pdf = ... return File(pdf, "application/pdf", "file.pdf"); } 
+2
source

@Darin is a great help always on this forum. His answer saved my day. Just extending his answer to include formats and stuff that I did. This is in response to his "Now, if the controller action uses the Content-Disposition header for attachment, it will prompt the user to download the file:"

  case "PDF": HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".pdf"); break; case "XML": HttpContext.Current.Response.AddHeader("Content-Type", "application/xml"); break; case "MHTML": HttpContext.Current.Response.AddHeader("Content-Type", "message/rfc822"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".mhtml"); break; case "EXCEL": HttpContext.Current.Response.AddHeader("Content-Type", "application/vnd.ms-excel"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".xls"); break; case "WORD": HttpContext.Current.Response.AddHeader("Content-Type", "application/msword"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".doc"); break; case "CSV": HttpContext.Current.Response.AddHeader("Content-Type", "text/csv"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportName + ".csv"); break; case "HTML4.0": HttpContext.Current.Response.AddHeader("Content-Type", "message/rfc822"); ; break; 
0
source

Source: https://habr.com/ru/post/1381645/


All Articles