oss 上传照片

impor thttputilfrom'../httputil'

export default{

props:{

multiple:{

type:Boolean,

twoWay:false

},

id:{

type:String,

twoWay:false

}

},

data(){

return{

url:'',

region:'oss-cn-shanghai', //账号

bucket:'yidao-cdn',

};

},

methods:{

doUpload(){

httputil.doRequest('/Aliyun/stsServiceSample',{'type':8},1,this.onSuccess,this.onError,true) 

//请求后台 获得Token KEYID 等。。

},

onSuccess(res){

//成功后处理

constfiles=document.getElementById(this.id);

constclient=newOSS.Wrapper({

region:this.region,

accessKeyId:res.accessKeyId,

accessKeySecret:res.accessKeySecret,

stsToken:res.securityToken,

bucket:this.bucket

})

if(files.files){

const fileLen=document.getElementById(this.id).files

const resultUpload=[];

for(leti=0;i

constfile=fileLen[i];

console.log(file)

let name=this.randName() //随机名

conststoreAs='/upload/masterDesc/'+name+'.jpg';

//上传

client.multipartUpload(storeAs,file).then((results)=>{

this.url=res.url+'/'+name+'.jpg'

this.$emit('getUrl',this.url) // 传值给父组件

}).catch((err)=>{

console.log("错误信息="+err);

});

}

}

},

onError(res){

},

randName(){

varchars='ABCDEFGHJKMNP_QRSTWXYZabcdefhijkmnprstwxyz2345678';

varmaxPos=chars.length;

varpwd='';

for(vari=0;i<13;i++) {

pwd+=chars.charAt(Math.floor(Math.random()*maxPos));

}

returnpwd;

}

}

};

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容