I am getting below response from AJAX respose:
this is response of zip file. please let me know how to save this filename.zip in Javascript. Inside the ZIP there is PDF file.
MY code is like this:
$.ajax({ url: baseURLDownload + "/service/report-builder/generateReportContentPDF", beforeSend: function (xhr) { xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); xhr.responseType = 'arraybuffer' }, type: "POST", data: JSON.stringify(parameter), contentType: "application/json", success: function(result) { console.log("ssss->"+result); var base64String = utf8_to_b64(result); //window.open("data:application/zip;base64,"+base64String); // It will download pdf in zip var zip = new JSZip(); zip.add("PDFReport.pdf", result); content = zip.generate(); location.href="data:application/zip;base64," + content; $.mobile.loading('hide'); }, error: function(xhr){ console.log("Request Status: " + xhr.status + " Status Text: " + xhr.statusText + " " + xhr.responseText); $.mobile.loading('hide'); showAlert("Error occured. Unable to download Report", "Message", "OK"); } });
RESPOSE Console.log("ssss->"+result);
PK��Q��F���������������/crt_pdf_10204725.pdf��uX\M�8|p�����݃�;w�@p �ܝBp��݂�;|C�ھ�w������=O���]]�%�N�����#+�reup����������Y������̉�J����3)� O��C����F�M�P�&�����rA�@��7T.��z(%h��x�x0�0Z�-i��%q�e�M�����i�"�c��-/��j��齔/ļL瞄�0� �� >�o��[��6 멆�n��s�$� �#>˘ '��wT�� ���3�36DK�+�̓�t6 ��r��sA:���x�<>n������'U��RLqA+���ݺ�BM��:4ĞP�}���:�}ߣP����?F)�9-�W0���2�{x��#2v8N.$V�>X=/�+�c}���ּ�\y���\*�J\�� ���90�T�L� 3p���*Sfj(���PWWz��O�s�9]&�
���iO|�9�;�5��ʘdW�cl% �%;����u���%[�5������Q]$��[L>���yXg�9��2+&,iFs�Q�����u�.�E(�>W��+��M ؟E������i|���k�k�c蟴CcG�j��4s|x �F
1�}��Y��,29�0M=-O����m\L��y��^On^���\���u��a���F9:zc�Sy�-�g��fu�n�C�T:{ ��4&/ ��LM9�98� �&Pnc�!��m�r�~��)74�04��0�0������M�~"��.ikjG��M�-
You can then write the file using one of two methods: either by converting the zip file to a Node. js buffer using toBuffer() , by using file. writeZip() . // One way to write the zip file: convert it to a buffer and use `fs` const fs = require('fs'); fs.
An Open Office Write file is binary as it is a zipped set of XML files, but the XML files inside are considered text files. Even though they contain both text and characters that represent font-size and color.
JavaScript can handle binary data via typed arrays. And here is a library for dealing with binary files, that you can use as a reference point for your application. How quick is JavaScript - I'm not sure that this is the right question. It depends on browser, and user`s machine.
Finally I got answer of my question:
Here is the code:
var xhr = new XMLHttpRequest(); xhr.open("POST", baseURLDownload + "/service/report/QCPReport", true); xhr.setRequestHeader("Content-type","application/json"); xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { // alert("Failed to download:" + xhr.status + "---" + xhr.statusText); var blob = new Blob([xhr.response], {type: "octet/stream"}); var fileName = "QCPReport.zip"; saveAs(blob, fileName); } } xhr.responseType = "arraybuffer"; xhr.send(JSON.stringify(QCPParameter));
No dependancy.
Compatible with IE 10,11, Chrome, FF and Safari:
function str2bytes (str) { var bytes = new Uint8Array(str.length); for (var i=0; i<str.length; i++) { bytes[i] = str.charCodeAt(i); } return bytes; } var xhr = new XMLHttpRequest(); xhr.open("POST", baseURLDownload + "/service/report/QCPReport", true); xhr.setRequestHeader("Content-type","application/json"); xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { // alert("Failed to download:" + xhr.status + "---" + xhr.statusText); var blob = new Blob([str2bytes(xhr.response)], {type: "application/zip"}); var fileName = "QCPReport.zip"; if (navigator.msSaveOrOpenBlob) { navigator.msSaveOrOpenBlob(blob, filename); } else { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display:none"; var url = window.URL.createObjectURL(blob); a.href = url; a.download = filename; a.click(); window.URL.revokeObjectURL(url); a.remove(); } } } xhr.responseType = "arraybuffer"; xhr.send(JSON.stringify(QCPParameter));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With