美文网首页
Vue Upload 兼容IE9

Vue Upload 兼容IE9

作者: ErHu丶 | 来源:发表于2020-04-14 20:28 被阅读0次

el-upload 并不支持IE9环境, 于是自己简单封装了一个支持IE9的Upload组件(基于 Web Uploader)

效果:


上传中
上传完成

组件:

<template>
  <div class="upload-container">
    <div id="upload" style="display: inline-block;">
      <el-button size="small" :loading="showPercentage" type="primary">{{title}}</el-button>
    </div>
    <el-tag
      v-if="uploadFile"
      size="small"
      type="success"
      closable
      :title="uploadFile.name"
      @close="removeUploadFile">
      {{ uploadFile.name | nameFilter }}
    </el-tag>
    <el-progress
      v-if="showPercentage"
      style="display: inline-block; width: 100px;"
      :percentage="percentage"
      :color="uploadColorMethod">
    </el-progress>
  </div>
</template>

<script>
export default {
  name: 'UploadButton',
  filters: {
    nameFilter(name) {
      if (name.length > 20) return name.substring(0, 20) + '...'
      return name
    }
  },
  props: {
    title: {
      type: String,
      require: false,
      default: () => '上传'
    },
    // 上传之前的回调方法, 该方法可返回自定义的上传参数
    beforeUpload: {
      type: Function,
      require: true,
      default: () => ''
    }
  },
  data() {
    return {
      showPercentage: false,
      percentage: 0,
      uploadFile: '',
      uploadData: {}
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initUpload()
    })
  },
  methods: {
    initUpload() {
      const _this = this
      // eslint-disable-next-line
      const uploader = WebUploader.create({
        // 选完文件后,是否自动上传。
        auto: true,
        // swf文件路径, 此文件需要放到public文件夹下, 这样打包后dist中的vendor下有Uploader.swf文件
        // 注意文件路径是否跨域, 否则IE9下上传按钮将失效
        swf: window.location.href.split('/#')[0] + '/vendor/uploader.swf',
        // 文件接收服务端。
        server: '/YourPath',
        pick: {
          // 选择文件的按钮。可选。
          id: '#upload',
          // 是否支持多选
          multiple: false
        },
        // 允许类型, 必填, 否则IE9下上传按钮将失效
        accept: {
          title: 'Excel',
          extensions: 'xls,xlsx',
          mimeTypes: ''.XLS,.xlsx''
        },
        // 支持重复上传
        duplicate: true
      })
      uploader.on('beforeFileQueued', function (file) {
        if (_this.beforeUpload) {
          _this.beforeUpload(file).then(uploadData => {
            _this.uploadData = uploadData
            return true
          })
        } else {
          return true
        }
      })
      uploader.on('uploadBeforeSend', function (object, data, headers) {
        // 移除默认的参数
        _this.showPercentage = true
        delete data.id
        delete data.name
        delete data.type
        delete data.lastModifiedDate
        delete data.size
        // jq 的扩展, 可以用其他方法代替
        data = $.extend(data, _this.uploadData)
      })
      uploader.on('uploadProgress', function (file, percentage) {
        if (percentage === 1) percentage = 0.99
        _this.percentage = percentage * 100
      })
      uploader.on('uploadComplete', function(file) {
        _this.percentage = 100
      })
      uploader.on('uploadSuccess', function (file, res) {
        _this.showPercentage = false
        if (res.status === 200) {
          // 保存当前文件
          _this.uploadFile = file
          _this.$message.success('上传成功')
          _this.$emit('onSuccess', res, file)
        } else {
          _this.$emit('onError')
          _this.$message.error('上传失败:' + res.message)
        }
      })
      uploader.on('uploadError', function (file) {
        _this.showPercentage = false
        _this.$message.error('上传失败')
        _this.$emit('onError')
      })
    },
    removeUploadFile() {
      this.uploadFile = ''
      this.$emit('onRemove')
    },
    uploadColorMethod(percentage) {
      if (percentage < 50) {
        return '#909399'
      } else if (percentage < 100) {
        return '#e6a23c'
      } else {
        return '#67c23a'
      }
    }
  }
}
</script>

<style type="css">
  .webuploader-container {
    position: relative;
    vertical-align: middle;
  }
  .webuploader-element-invisible {
    position: absolute !important;
    clip: rect(1px 1px 1px 1px);
    clip: rect(1px,1px,1px,1px);
  }
  .webuploader-pick {
    position: relative;
    display: inline-block;
    cursor: pointer;
    text-align: center;
  }
</style>

使用:

<!-- index.html 添加 -->
<script src="//cdn.staticfile.org/webuploader/0.1.5/webuploader.js"></script>
<upload-button
  title="名单上传"
  class="upload-demo"
  :before-upload="beforeUpload"
  @onSuccess="onSuccess"
  @onRemove="onRemove"
  @onError="onError">
</upload-button>

methods: {
  beforeUpload(file) {
    const uploadData = {
      fileName: file.name
    }
    return new Promise((resolve) => {
      this.$nextTick(() => {
        resolve(uploadData)
      })
    })
  },
  onSuccess(response, file) {
  }
}

特殊说明:

1. .swf 文件的地址注意是否跨域, 否者IE上传无效. 最好跟着打包文件走.
2. accept 要写就把子属性写全, 否者IE上传无效
3. Demo里的是Excel文件, 如需其他文件自行修改 accept 类型
多看官方文档

相关文章

  • Vue Upload 兼容IE9

    el-upload 并不支持IE9环境, 于是自己简单封装了一个支持IE9的Upload组件(基于 Web Upl...

  • vue+axios+vue-router兼容ie9

    参考:Vue 兼容 ie9 的全面解决方案 前序 项目突然要求兼容ie9...可是用到的技术栈对ie9似乎不是很友...

  • vue-cli兼容ie9的兼容问题

    当vue遇见ie9的时候,部署到服务器之后,打开居然是一片空白,vue是支持ie9的,这个时候就需要来一波兼容了参...

  • vue ie9兼容

    要实现其实很简单,只需要我们下载一个babel-polyfill, npm install babel-polyf...

  • vue兼容ie9

    突然来的项目上的需求 我们的产品要在ie9上适用,于是 前端朋友们就非常头疼了 身为一个前端开发 估计最头疼的就是...

  • vue3.0 变化

    Proxy API vue2.X 中的 defineProperty API优点:兼容性好,支持 IE9缺点:Ob...

  • Vue 在IE9中兼容上传,安全提示"X-Frame-Opti

    截止写稿,已经Vue在2.6.9的版本中已经支持IE9,不在需要polyfills 说下问题 项目需要兼容到IE9...

  • 打造炫酷的progressbar(上限可大于100%)

    HTML + CSS + JS 原生实现, 兼容 Chrome, Firefox, IE9 及以上, 兼容 PC,...

  • IE9以下版本兼容h5标签方法

    主流浏览器Chrome、Firefox、Safari等能够兼容h5+c3,但是IE9以下完全不兼容,IE9也是部分...

  • css3课件

    第一章 字体属性1:css3属性具有兼容性,不兼容IE9以下浏览器,IE9部分兼容。 2:各大浏览器的内核兼容写法...

网友评论

      本文标题:Vue Upload 兼容IE9

      本文链接:https://www.haomeiwen.com/subject/azhxvhtx.html