Vue Upload 兼容IE9

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 类型
多看官方文档
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 突然来的项目上的需求 我们的产品要在ie9上适用,于是 前端朋友们就非常头疼了 身为一个前端开发 估计最头疼的就是...
    有一个程序媛阅读 8,265评论 7 2
  • 短短前小半生遇到几个懂自己的人,我很知足,也很幸运
    希望能活的善良阅读 150评论 0 2
  • 1、调研走访 繁忙地工作、紧张的生活几乎让每个人都想透透气。五一前,部门利用了一天的时间,先是利用一个下午的时间到...
    苏吉儿阅读 276评论 0 2
  • 毕升者,徽州人也。升少时印刷于工坊,字必亲刻,然错误者十有一二,必废之再刻。刻完之后,其刻板汗牛充栋,存之占地,弃...
    胡诌文学阅读 369评论 0 8
  • 无论怎么样,一个人借故堕落总是不值得原谅的, 越是没有人爱,越要爱自己。 ——亦舒 ​ 李宗盛《晚婚》,等待和错过...
    苏小逸阅读 112评论 0 1