文件下载失败,如何将blob对象处理成json

axios[method](url, data, {
	responseType: 'blob'  // blob arraybuffer
}).then(res => {
	let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'});
	
	/* 读取异常状态提示 */
	var reader = new FileReader();
	reader.readAsText(blob);
	reader.onload = e => {
		//判断接口是否返回失败信息 
		if((e.target.result.indexOf('message') !== -1) && JSON.parse(e.target.result).error === 'OK') {
			this.$message({
				type: 'info',
				message: JSON.parse(e.target.result).message
			})
		} else {
			let name = "fileName.xls";
			
			if (!!window.ActiveXObject || "ActiveXObject" in window) {
				window.navigator.msSaveOrOpenBlob(blob, name);
			} else {
				const link = document.createElement('a');
				link.style.display = 'none';
				link.href = URL.createObjectURL(blob);
				link.setAttribute('download', name);
				document.body.appendChild(link);
				link.click();
				document.body.removeChild(link);
			}
			resolve(res)
		}
	}  
}).catch(err => {
   reject(err)
})

你可能感兴趣的:(Javascript)