您好,请查阅以下两篇文档:
我也写了一个 Demo 供您参考:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue upload demo</title>
</head>
<body>
<div id="app">
<div>
<input type="file" @change="handleChangeFile" />
<button @click="handleUpload" :disabled="uploading">Upload</button>
</div>
<ul>
<li v-for="url in urls" :key="url">
<a :href="url" target="_blank">{{ url }}</a>
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
<script src="https://cdn.jsdelivr.net/npm/leancloud-storage@4.10.0/dist/av-min.js"></script>
<script>
AV.init({
appId: 'please-replace-with-your-appId',
appKey: 'please-replace-with-your-appKey',
serverURL: 'https://please-replace-with-your-customized.domain.com',
});
new Vue({
el: '#app',
data: {
file: null,
urls: [],
uploading: false,
},
methods: {
handleChangeFile(e) {
if (e.target.files.length) {
this.file = e.target.files[0]
}
},
handleUpload() {
if (!this.file) {
return
}
this.uploading = true
new AV.File(this.file.name, this.file)
.save()
.then((file) => this.urls.push(file.url()))
.catch(console.error)
.finally(() => this.uploading = false);
},
},
});
</script>
</body>
</html>