这个是一个Vue中用阿里云提供的SDK实现上传文件至OSS的demo,只有功能实现,没有美观度可言,希望给大家提供一点帮助
1.安装依赖
1
| npm install ali-oss --save
|
2.代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <template> <div id="app"> <input type="file" @change="getInfo($event)"> <h2>path:{{ path }}</h2> <button @click="put">上传</button> </div> </template>
<script> const oss = require('ali-oss'); const client = oss({ accessKeyId: 'your access key', accessKeySecret: 'your access secret', bucket: 'your bucket name', region: 'your region' });
export default { name: 'App', data() { return { path: '', OSSPath: 'test/', file: {} } }, methods: { getInfo(e) { this.path = e.target.value; this.file = e.target.files[0] }, async put() { if (this.path !== '') { let textArray=this.path.split('.') let extension = '.' + textArray[textArray.length-1] try { let result = await client.put(this.OSSPath + Date.parse(new Date()) + extension, this.file); console.log(result); console.log(result.url); alert('上传成功') } catch (e) { console.log(e); alert("上传失败") } } else{ alert("请先选择要上传文件") } } } } </script>
<style> </style>
|