Dpk_Gopi
Dpk_Gopi

Reputation: 267

How to convert the byte code to zip file in javascript

Im using the following code to convert the byte code to zip file but it does not download the actual xml and asc file send in the response , instead it just donwloads the standard zip contents , im not aware where i am going wrong , can anyone help me with this,

 $http({
    url:url,
    method: requestType,
    data:requestBody?requestBody:"",
    headers: {
       'Content-type': "application/json",
       "SessionID":$rootScope.token
    },
    responseType: 'arraybuffer'
}).success(function (data, status, headers, config) {
    let blob = new Blob([data], {type: "application/zip"});
    let objectUrl = URL.createObjectURL(blob);
    let link = document.createElement('a');
    link.href = objectUrl;
    link.download = fileName;
    link.click();
    window.URL.revokeObjectURL(link.href);
    $scope.exp = {}; // to reset the export form after submit.
    $scope.surveyReportdownloading = false;
    $scope.tabActive = false;
 }).error(function (error) {
    //upload failed
    console.log(error);
});

this is not downloading the actual files at all. Can anyone help me through this. The byte cod ethat backend team is sending is as follows.

"PK:8xJMÆVÞ|xl/workbook.xml ¢( ÍnÂ0ïúÖ>@MpR­¸{C,²vdѾ}CR¢¶'n;³³fË«u磵göI­ñ«    ¡+8÷²AþÅvhú]mÐKwⶮµÄBxEwØ ­ñî<´GX¾s(oµ#6°|~b¬¼5;h¼úAöƽîÍd|ñ¿©rMbFVð~!îØ`nT10Wè~Ø4SäiÅÑ,ÇWøÁÿC|¼í¶ëÃzûL/ó4KËYZG0U:üþÂòPK:8xJnmt?Ø[Content_Types].xml ¢( ÅMNÃ0¯y·] vl¡\À²'ÕøGIiÏÆ#q& TUЪº²lÏ{ßõä·÷é|ãl
mð¥#×ÁX¿,EKU~#æ³éË6f\ê±5Q¼u

Na"x¾©Brx2*½RKÑèZêà <åÔyÙôÕ6=løxÀ²\dwC]±±Z_˵7¿ y¨*«ÁÝ:(5¹¦è×Â)ë¯zc¹ Áã _S¬ìk°¶w'~Äd d èQ9öF¾´êBÙ/ãâ¼ÃîüÿkiÛ>þfå"Ç㿽Sç =ÉÞ' ]d £áºE îdþ`s(}Oâ&K\­gJü=x?½wÈþ}PK 38xJ£ ²× rels/.rels ¢( ï»¿PK:8xJILE@¥¶xl/worksheets/sheet1.xml ¢( ¥ÛrÇEÅ÷èn\U\¡\q®ª%^ÿþõ˯ûÃ/·W»Ýñìÿ|"

Any help is appreciated. Thanks!

Upvotes: 1

Views: 8876

Answers (1)

Gangadhar Jannu
Gangadhar Jannu

Reputation: 4414

Seems like the issue is with the type parameter try with the below code

You can access the content-type from headers.

If it doesn't work, try with application/zip, application/octet-stream

$http({
  url: url,
  method: requestType,
  data: requestBody ? requestBody : "",
  headers: {
    'Content-type': "application/json",
    "SessionID": $rootScope.token
  },
  responseType: 'arraybuffer'
}).success(function(data, status, headers, config) {
  let blob = new Blob([data], {
    type: headers['content-type']
    //               OR
    // type:"application/zip, application/octet-stream"
  });
  let objectUrl = URL.createObjectURL(blob);
  let link = document.createElement('a');
  link.href = objectUrl;
  link.download = fileName;
  link.click();
  window.URL.revokeObjectURL(link.href);
  $scope.exp = {}; // to reset the export form after submit.
  $scope.surveyReportdownloading = false;
  $scope.tabActive = false;
}).error(function(error) {
  //upload failed
  console.log(error);
});

 var blob = new Blob([response.data],{type:headers['content-type']});
  var link = document.createElement('a');
  link.href = window.URL.createObjectURL(blob);
  link.download = "Filename";
  link.click();

Upvotes: 2

Related Questions