新增JS页面 axiosExport.JS // Axios拦截请求并实现下载import axios from 'axios'// download urlexport const downloadUrl = (url) = { console.log(url) let iframe = document.createElement('iframe') iframe.style.display = 'none' ifram
新增JS页面
axiosExport.JS
// Axios拦截请求并实现下载 import axios from 'axios' // download url export const downloadUrl = (url) => { console.log(url) let iframe = document.createElement('iframe') iframe.style.display = 'none' iframe.src = url iframe.onload = function () { document.body.removeChild(iframe) } document.body.appendChild(iframe) } // Add a response interceptor // res返回的东西可以跟后端确认 axios.interceptors.response.use(res => { if (res.data.status && res.data.status === 300) { return '300' // 链接正确,下载失败 } else { downloadUrl(res.request.responseURL) return '200' // 链接正确,下载成功 } }, error => { // Do something with response error return error // 链接错误 }) export default axios
VUE页面
import axios from './axiosExport' // 导出 或 下载 exportDoc () { let URL = `下载地址` let me = this axios.get(URL).then(function (response) { if (response === '200') { me.$message.success('下载成功!') } else { me.$message.warning('下载失败!') } }).catch(function (response) { console.log(response); }); }
以上这篇在VUE中实现文件下载并判断状态的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。