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
| <template> <div v-if="imageUploadProgress"> <span class="image-upload-progress">{{ imageUploadProgress + '%' }}</span> </div> </template>
<script> async createFile(file, postId) { // 创建表单 const formData = new FormData();
// 添加字段 formData.append('file', file);
// 上传文件 try { const response = await apiHttpClient.post( `/files?post=${postId}`, formData, { headers: { Authorization: `Bearer ${this.token}`, }, onUploadProgress: event => { //使用这个函数获取到文件的大小和已上传大小 console.log(event);
const { loaded, total } = event;
this.imageUploadProgress = Math.round((loaded * 100) / total); //这里计算出进度 }, }, );
// 清理 this.file = null; this.imagePreviewUrl = null; this.$refs.file.value = ''; this.imageUploadProgress = null; console.log(response.data); } catch (error) { this.errorMessage = error.message; } }, </script>
|