upload.vue 1.0 KB
Newer Older
P
Pan 已提交
1
<template>
P
Pan 已提交
2
  <el-upload action="https://upload.qbox.me" :data="dataObj" drag :multiple="true" :before-upload="beforeUpload">
P
Pan 已提交
3 4 5 6 7 8 9
    <i class="el-icon-upload"></i>
    <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  </el-upload>
</template>


<script>
P
Pan 已提交
10
import { getToken } from 'api/qiniu' // 获取七牛token 后端通过Access Key,Secret Key,bucket等生成token
P
Pan 已提交
11 12
    // 七牛官方sdk https://developer.qiniu.com/sdk#official-sdk

P
Pan 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
export default{
  data() {
    return {
      dataObj: { token: '', key: '' },
      image_uri: [],
      fileList: []
    }
  },
  methods: {
    beforeUpload() {
      const _self = this
      return new Promise((resolve, reject) => {
        getToken().then(response => {
          const key = response.data.qiniu_key
          const token = response.data.qiniu_token
          _self._data.dataObj.token = token
          _self._data.dataObj.key = key
          resolve(true)
        }).catch(err => {
          console.log(err)
          reject(false)
        })
      })
P
Pan 已提交
36
    }
P
Pan 已提交
37
  }
P
Pan 已提交
38
}
P
Pan 已提交
39
</script>