七牛云上传及上传要领封装

分片及七牛云上传封装
项目内里用到七牛云,有分片和简朴上传

在此做下简朴的纪录,分享

下面是分片上传封装

process.env.MAX_FILESIZE 是我写在config内里分片的大小 如1024102410 10MB分片

项目是vue框架,要求是axios。

上传的进度在onUploadProgress中猎取


/**
 * 封装upload要领
 * @param url 上传途径
 * @param json 包括file对象
 * @returns {Promise}
 */
export function upload(url,json) {
  return new Promise((resolve,reject) => {
      let size = parseInt(Number(json.file.size)/Number(process.env.MAX_FILESIZE) +1);
      let y = 0;
      let fun = function(){
        let formData = new FormData();
        let file = json.file.slice(y*process.env.MAX_FILESIZE,(y+1)*process.env.MAX_FILESIZE);
        formData.append('chunk', y);
        formData.append('file_name', json.file.name);
        formData.append('count', size);
        formData.append('type', json.type);
        formData.append('file', file);
        axios.post(url,formData,{ headers: {
            'Content-Type': 'multipart/form-data'
          },timeout:10000000,
          onUploadProgress: progressEvent => {
            //progressEvent.loaded 为上传进度
          },
        }).then(response => {
            if(response.data.code === 200){
              if(y+1<size){
                y++;
                fun()
              }else{
                y=0;
                resolve(response.data.data);
              }
            }else{
              Vue.prototype.$message.error(response.data.msg)
            }
          })
          .catch(err => {
            reject(err);
            let message = '要求失利!请搜检收集';
            if(err.response)message=err.response.data.message;
            Vue.prototype.$$msgbox({
              title:'毛病!',
              message:message,
              type:'error',
            })
          })
      };
      fun()
    }

  })
}

  

下面是七牛云上传的封装


/**
 * 封装upload要领
 * @param json json中包括fail
 * @returns {Promise}
 */
export function uploadQiniu(json) {
  return new Promise((resolve,reject) => {
axios.get('猎取七牛权限的背景接口地点,重要猎取七牛token',{
        params:{
          file_name:json.file.name
        }
      }).then(resData =>{
          let putExtra = {
            fname: json.file.name,
            mimeType:json.mimeType || null
          };
          let congif = {};
          let observable = qiniu.upload(json.file,resData .data.data.new_name,resData .data.data.token,putExtra,congif);
          let observer = {
            next(res){
              let progress =  Number(res.total.percent.toFixed(0));
              // 此处获得上传进度百分比 可加后续操纵
            },
            error(err){
              reject(err);
              let message = '要求失利!请搜检收集';
              if(err.response)message=err.response.data.message;
              Vue.prototype.$alert({
                title:'毛病!',
                message:message,
                type:'error',
              })
            },
            complete(res){
              res.url = res.key;
              res.name = json.file.name;
              resolve(res);
            }
          };
          let subscription = observable.subscribe(observer)
        }
      );
    })

}

    原文作者:王_ye
    原文地址: https://segmentfault.com/a/1190000018436526
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞