基于Vue和Quasar的前端SPA我的项目实战之文件上传(十)

回顾

通过之前一篇文章 基于Vue和Quasar的前端SPA我的项目实战之数据导入(九)的介绍,实现了业务数据批量导入性能,本文次要介绍文件上传相干内容。

简介

crudapi反对附件字段,表字段外面保留的是文件url字符串。附件能够通过其它文件管理系统比方阿里云的OSS进行上传,或者应用零碎自带的文件治理API进行上传,包含一般文件上传和大文件切片上传两种形式。

UI界面


文件上传


大文件上传

API

文件上传API,包含一般文件上传和大文件切片两个性能,具体的通过swagger文档能够查看。通过axios封装api,名称为file

import { axiosInstance } from "boot/axios";const HEADERS = {  "Content-Type": "multipart/form-data"};const file = {  upload: async function(data, progressCallback) {    console.log("file->upload")    return axiosInstance.post(`/api/file` , data,      {        headers: HEADERS,        onUploadProgress:  (progressEvent) => {          if (progressCallback) {            progressCallback(progressEvent)          }        }    });  },  bigUpload: async function(data, progressCallback) {    console.log("file->bigUpload")    return axiosInstance.post(`/api/file/big` , data,      {        headers: HEADERS,        onUploadProgress:  (progressEvent) => {          if (progressCallback) {            progressCallback(progressEvent)          }        }    });  }};export { file };

外围代码

CFile组件

 <q-toggle v-model="enableBigFile" label="开启大文件上传模式" />  <div v-show="!enableBigFile" class="q-py-md">    <q-file v-model="normalFile" label="请抉择文件(一般上传)">      <template v-slot:prepend>        <q-icon name="attach_file" />      </template>      <template v-slot:after>        <q-btn round dense flat icon="send" @click="onSubmitClick" />      </template>    </q-file>  </div>  <div v-show="enableBigFile" class="q-py-md">    <q-file v-model="bigFile" @input="bigFileAdded" label="请抉择文件(大文件上传)">      <template v-slot:prepend>        <q-icon name="attach_file" />      </template>      <template v-slot:after>        <q-btn round dense flat icon="flight_land" @click="onBigSubmitClick" />      </template>    </q-file>  </div>

通过toggle切换上传模式,如果是小文件采纳一般的形式即可。

一般上传

async onSubmitClick() {  console.info("CFile->onSubmitClick");  if (!this.normalFile) {    this.$q.notify({      message: '请抉择文件!',      type: 'warning'    });    return;  }  this.$q.loading.show({    message: "上传中"  });  try {    let form = new FormData()    form.append('file', this.normalFile);    this.fileInfo = await fileService.upload(form, (e)=> {      console.info(e);    });    this.$q.loading.hide();    this.$emit("input", this.fileInfo);  } catch (error) {    this.$q.loading.hide();    console.error(error);  }}

大文件切片上传

bigFileAdded(f) {  console.info("CFile->fileAdded");  if (!f) {    console.info("CFile->cancel");    return;  }  this.$q.loading.show({    message: "文件筹备中"  });  FileMd5(f, this.chunkSize, (e, md5) => {    this.md5 = md5;    console.info(e);    console.info(md5);    this.$q.loading.hide();  });},async onBigSubmitClick() {  console.info("CFile->onBigSubmitClick");  if (!this.bigFile) {    this.$q.notify({      message: '请抉择文件!',      type: 'warning'    });    return;  }  this.$q.loading.show({    message: "上传中"  });  try {    let chunks = this.getChunks();    let reqs = [];    for (let i = 0; i < chunks; ++i) {      reqs.push(this.uploadWithBlock(i));    }    await Promise.all(reqs)    .then((datas) => {      console.info(datas);      this.checkFinished(datas);    });  } catch (error) {    this.$q.loading.hide();    console.error(error);  }}

大文件如果采纳一般的上传形式,可能因为网络的起因速度比较慢,而且不稳固,所以采纳切片的形式进行多线程上传。具体原理如下:首先计算文件MD5,后盾会依据MD5惟一确定是同一个文件,同一个文件的不同block依据大小和偏移量会写在雷同文件对应的地位,当最初一个block上传胜利后,示意上传完结。分片大小默认为20MB,能够配置为须要的值,前端通过Promise.all的ajax调用形式能够实现多线程同时上传。

文件表为例


文件表的“链接”字段设置类型为“附件ATTACHMENT”,增加业务数据页面会主动采纳CFile组件。


抉择大文件之后,点击上传图标,通过chrome网络申请发现,多线程分片上传模式曾经启动,上传完结之后能够查看下载。

小结

本文次要介绍了文件上传性能,包含一般上传模式和大文件切片上传模式,大文件切片上传模式通过优化后很容易反对断点续传和秒传,后续会依据需要优化文件上传性能。

demo演示

官网地址:https://crudapi.cn
测试地址:https://demo.crudapi.cn/crudapi/login

附源码地址

GitHub地址

https://github.com/crudapi/crudapi-admin-web

Gitee地址

https://gitee.com/crudapi/crudapi-admin-web

因为网络起因,GitHub可能速度慢,改成拜访Gitee即可,代码同步更新。