Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save binary data of zip file in Javascript?

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 �F1�}��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�-

like image 691
Nishant Singh Avatar asked Jun 30 '15 00:06

Nishant Singh


People also ask

How do I save a node JS zip file?

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.

Are zip files binary?

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.

How does JavaScript handle binary data?

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.


2 Answers

Finally I got answer of my question:

  • https://github.com/eligrey/Blob.js/
  • https://github.com/eligrey/FileSaver.js/

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)); 
like image 104
Nishant Singh Avatar answered Sep 25 '22 17:09

Nishant Singh


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)); 
like image 23
Below the Radar Avatar answered Sep 23 '22 17:09

Below the Radar