栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

接收Zip文件作为对AJAX请求的响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

接收Zip文件作为对AJAX请求的响应

一种利用的方法

XMLHttpRequest()
;
检查
a
element是否具有
download
属性,如果为true,则将
download
property 设置为
objectURL
;
否则,使用
window.open()
带参数
objectURL
Blob
响应

function downloadFile(url, headers, filename) {  function handleFile(data) {    console.log(this.response || data);    var file = URL.createObjectURL(this.response || data);    filename = filename || url.split("/").pop();    var a = document.createElement("a");    // if `a` element has `download` property    if ("download" in a) {      a.href = file;      a.download = filename;      document.body.appendChild(a);      a.click();      document.body.removeChild(a);    } else {      // use `window.open()` if `download` not defined at `a` element      window.open(file)    }  }  var request = new XMLHttpRequest();  request.responseType = "blob";  request.onload = handleFile;  request.open("GET", url);  for (var prop in headers) {    request.setRequestHeader(prop, headers[prop]);  }  request.send();}downloadFile("/path/to/resource/", {"x-content": "abc"}, "filename.zip")

使用jQuery版本叉的

jquery-ajax-blob-arraybuffer.js

// use this transport for "binary" data type$.ajaxTransport("+binary", function(options, originalOptions, jqXHR){    // check for conditions and support for blob / arraybuffer response type    if (window.FormData && ((options.dataType && (options.dataType == 'binary'))         || (options.data         && ((window.ArrayBuffer && options.data instanceof ArrayBuffer)         || (window.Blob && options.data instanceof Blob))))       )    {        return { // create new XMLHttpRequest send: function(headers, callback){        // setup all variables     var xhr = new XMLHttpRequest(),        url = options.url,        type = options.type,        async = options.async || true,        // blob or arraybuffer. Default is blob        dataType = options.responseType || "blob",        data = options.data || null,        username = options.username || null,        password = options.password || null;     xhr.addEventListener('load', function(){ var data = {}; data[options.dataType] = xhr.response; // make callback and send data callback(xhr.status         , xhr.statusText         , data         , xhr.getAllResponseHeaders());     });     xhr.open(type, url, async, username, password);        // setup custom headers        for (var i in headers ) { xhr.setRequestHeader(i, headers[i] );        }     xhr.responseType = dataType;     xhr.send(data); }, abort: function(){     jqXHR.abort(); }        };    }});function downloadFile(url, headers, filename) {return $.ajax({  url:url,  dataType:"binary",  processdata: false,  headers:headers}).then(function handleFile(data) {    console.log(this.response || data);    var file = URL.createObjectURL(this.response || data);    filename = filename || url.split("/").pop();    var a = document.createElement("a");    // if `a` element has `download` property    if ("download" in a) {      a.href = file;      a.download = filename;      document.body.appendChild(a);      a.click();      document.body.removeChild(a);    } else {      // use `window.open()` if `download` not defined at `a` element      window.open(file)    }  })}downloadFile("/path/to/resource/", {"x-custom-header":"abc"}, "filename.zip");

只需下载就可以了

您可以使用

<a>
元素,
download
属性

$("<a>", {href: someUrl,        download: "filename.zip"}).appendTo("body")[0].click()

或者使用库解析文件,例如,从文件中包含的数据

zip.js
创建多个或单个可下载
.zip
文件。

创建每个文件的objectURL,使用

a
元素下载每个文件。

如果

download
浏览器不提供该属性,则可以使用类型设置为下载文件
data URI
的文件对象
MIME``application/octet-stream



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号