1

I am getting the file content as binary or byte array(FROM THE REST API) and want to convert this into a excel file and download it in client side using angular.js , but don't know how to do it. Can anyone help me out here.

Also im attaching the code here , .xls file is getting downloaded but after opening it file content remains same binary/byte content that im getting from a api response , instead of the actual file content. Can anyone tell what i am missing here. Or how i can convert/read the response to actual format. Any help is much appreciated as im completely stuck.

$http.get(__env.apiUrl+"/UserSurvey/GetTrackingReport?surveyId="+$rootScope.surveysummaryID,{headers:{"Content-type":"application/json",'sessionID':$rootScope.token}}).then(function(response){
          console.log(response);
             var blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});
              if (window.navigator && window.navigator.msSaveBlob) {
                  window.navigator.msSaveBlob(blob);
              }
              else {
                  var objectUrl = URL.createObjectURL(blob);
                  window.open(objectUrl);
              }

        },function(error){
            console.log(error);
        });

Many Thanks,

1 Answer 1

1

If anyone faced the same issue , I had missed out on conversion of the unsignedint value and passes the same to the blob object and it works!!

   $http.get(__env.apiUrl+"/UserSurvey/GetTrackingReport?surveyId="+$rootScope.surveysummaryID,{headers:{"Content-type":"application/json",'sessionID':$rootScope.token,'Accept': "application/vnd.ms-excel"}}, {responseType: 'arraybuffer'}).then(function(response){
              var dec = window.atob(response.data);
              var myArr = new Uint8Array(dec.length)
              for(var i = 0; i < Object.keys(dec).length; i++){
                  myArr[i] = dec.charCodeAt(i);
              }
              var blob = new Blob([myArr], {type: 'application/vnd.ms-excel'});
              if (window.navigator && window.navigator.msSaveBlob) {
                  window.navigator.msSaveBlob(blob);
              }
              else {
                  var objectUrl = URL.createObjectURL(blob);
                  window.open(objectUrl);
              }
        },function(error){
            console.log(error);
        });

Hope this helps someone!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.