前端js下载zip文件并解压昨天做项目有个需求,后端返回的数据有个字段是url,前端要通过这个url下载一个3D模型文件,这个模型文件是zip文件(里面有个json文件),拿到这个zip文件还需要解压那到json文件!看下面怎么实现的!一. 后端返回文件流,前端必须进行转换下载,请求的方式很多,我下面随便写几种!1.原生js的function getBlob() { let file_url = "http://192.168.26.70/file/file/static/threeDModel/vacuumFlask/vacuumFlask.zip" let xhr = new XMLHttpRequest(); xhr.open("get", file_url, true); xhr.responseType = "blob"; xhr.onload = () => { if (this.readyState == 4 && this.status == 200) { console.log(this.response) } }; xhr.send(); }; getBlob();2.使用angularJS的$httplet templatedownload = () => {var url = 'http://192.168.26.70/file/file/static/threeDModel/vacuumFlask/vacuumFlask.zip';return $http({ method: "GET", url: url, responseType: "blob",})};templatedownload().then((response) => {console.log(response.data);});12345678910113.使用fetchfetch('http://192.168.26.70/file/static/threeDModel/vacuumFlask/vacuumFlask.zip').then(( response) => { console.log(response); return response.blob();//解析成blob对象}).then((data) => { console.log(data);});1234567二.利用jszip插件解压zip文件三.项目代码//3D模型下载接口处理class ThreeDPreviewBoardRestService { constructor( private restService: services.common.RestService, private $http: angular.IHttpService, private $cacheFactory: angular.ICacheFactoryService) { }threeDModelDownload(fileLocation: string): Promise