sailei1 / blog

1 stars 0 forks source link

canvas 多文件上传 #113

Closed sailei1 closed 3 years ago

sailei1 commented 3 years ago
 const random_string=(len=32)=>{
    var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
    var maxPos = chars.length;
    var pwd = "";
    for (var i = 0; i < len; i++) {
      pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
  }

//  直接文件流上传
 const canvasBlobUpload=(canvas,type)=>{
    return new Promise((resolve, reject)=> {
      canvas.toBlob((blob)=>{
        resolve(uploadClient.put(`${type}_${random_string()}`,blob))
      })
    })
  }
// 调用方法
const rs=[]
    for(let i=0;i<files.length;i++){
       const res= await canvasBlobUpload(files[i].file,files[i].type)
       rs.push({type:files[i].type,url:res.url})
    }