下载文件案例

传参数下载文件

 url= `/api/systemConfig/systemconfig-stationEquipment/expEquipmentListByStationIdAndIsgrp?id=${id}`
  var down = document.createElement('a');
  down.href = url;
  down.download = '';
  down.click();

不传参数下载文件

 window.open(`/api/systemConfig/systemconfig-stationEquipment/expEquipmentListByStationIdAndIsgrp`)

react自定义导入文件

customRequest:info=>{
        console.log(info)
        let formData = new FormData();
        formData.append("file",info.file);
        formData.append("patid",id);
        reqwest({
          url: '/api/systemConfig/systemconfig-stationEquipment/impEquipmentListByStationIdAndIsgrp',//上传url
          method: 'post',
          processData: false,
          data: formData,
          success: (res) => {//上传成功回调
            if (res.status) {
              Message.success(res.resultMsg);
              TableData()
            }
          },
          error: () => {//上传失败回调
            Message.error('上传失败!');
          },
        })
    }
/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url, parameter) {
  return axios({
    url: url,
    params: parameter,
    method: 'post',
    responseType: 'blob'
  })
}

/**
 * 下载文件
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @returns {*}
 */
export function downloadFile(url, fileName, parameter, type) {
  return downFile(url, parameter).then(data => {
    let fileReader = new FileReader()
    fileReader.onload = event => {
      try {
        let json = JSON.parse(event.target.result)
        if (!json.success) {
          Vue.prototype.$message.error(json.message || '下载文件错误')
          return
        }
      } catch (error) {
 
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]), fileName)
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + type)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      }
    }
    fileReader.readAsText(data)
  })
}
/**
 * 文件上传 用于富文本上传图片
 * @param url
 * @param parameter
 * @returns {*}
 */
export function uploadAction(url, parameter) {
  return axios({
    url: url,
    data: parameter,
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data' // 文件上传
    }
  })
}
/**
 * 获取文件服务访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar,subStr) {
  if(!subStr) subStr = 'http'
  try {
    if(avatar && avatar.startsWith(subStr)){
      return avatar;
    }else{
      if(avatar && avatar.length>0 && avatar.indexOf('[')==-1){
        return window._CONFIG['staticDomainURL'] + "/" + avatar;
      }
    }
  }catch(err){
   return;
  }
}

版权声明:本文为weixin_43796856原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。