JS obtains the file stream (packaged compressed file) returned from the background through get. How can I download it using JS?

the code is as follows

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "{-sharp$apiHost-sharp}/aw/export?brand_ids="+brand_ids,
  "method": "GET",
  "headers": {"token": "5f963175cc75613398e25ce2e5da56d7","os": "1"}
}
$.ajax(settings).done(function (data) {
    data
});
Mar.30,2021

window.location.href=' background interface address'


what is returned in the background is this. It cannot be downloaded with the location.href method, and the permission needs to be verified. If you do not need to verify the permission, you can use this location method directly

.

it's just not clear what to do with the returned file stream

.

you can try using Blob to convert to Url, and then download

using the a tag.

https://juejin.im/post/5b078f.


kick the problem back to the background. Why can't you download
Baidu network disk directly on this link? you can download everything with a single link. How can it be so troublesome



'use strict';

function xxxExportApi($http, $q) {
  return {
    download: function () {
      var deferred = $q.defer();
      var promise = deferred.promise;
      var url = 'api/xxx/download/xxx';
      $http({
        method: 'GET',
        url: url,
        responseType: 'arraybuffer'
      }).then(function (response) {
        var headers = response.headers();

        var filename = headers['content-disposition'].split(';')[1].split('=')[1];
        var contentType = headers['content-type'];

        var linkElement = document.createElement('a');
        try {
          var blob = new Blob([response.data], {
            type: contentType
          });
          var url = window.URL.createObjectURL(blob);

          linkElement.setAttribute('href', url);
          linkElement.setAttribute("download", filename);

          
          if (typeof (MouseEvent) == 'function') {
            var event = new MouseEvent("click", {
              "view": window,
              "bubbles": true,
              "cancelable": false
            });
            linkElement.dispatchEvent(event);
          } else if (navigator.appVersion.toString().indexOf('.NET') > 0) {
            window.navigator.msSaveBlob(blob, filename);
          }
          
          deferred.resolve();
        } catch (err) {
          //console.log(ex);
          deferred.reject(err);
        }
      }).catch(function (err) {
        //console.log(err);
        deferred.reject(err);
      });
      return deferred.promise;
    }
  }
}

 

the download solution of our project in angular1 is for reference only


directly use the submission of the form form, instead of using the get request of js, download it directly after submission

function DownLoad(url , data) {
 var $iframe = $('<iframe />');
  var $form = $('<form  method="get" target="_self"/>');
  $form.attr('action', url);
  for (var key in data) {
    $form.append('<input type="hidden"  name="' + key  + '" value="' + data[key] + '" />');
   }
  $iframe.append($form);
  $(document.body).append($iframe);
  $form[0].submit();
  $iframe.remove();
}
Menu