Commit 3078b2e4 authored by 以墨为白's avatar 以墨为白 🎧

单文件上传的优化

parent 16fc2df3
......@@ -19,21 +19,8 @@ var fileMd5Sum = (file) => {
});
}
const getFileChunk = (file, chunkSize) => {
const chunks = [];
const fileSize = file.size;
let currentChunkStart = 0;
while (currentChunkStart < fileSize) {
const chunkEnd = currentChunkStart + chunkSize;
const chunk = file.slice(currentChunkStart, chunkEnd);
chunks.push(chunk);
currentChunkStart = chunkEnd;
}
console.log("分片数量" + chunks.length);
return chunks;
};
const getFileMD5 = file => {
const getFileMD5Progress = (file, progress) => {
return new Promise((resolve, reject) => {
const chunkSize = 2097152; // 每次读取2MB.
const chunks = getFileChunk(file, chunkSize);
......@@ -42,7 +29,7 @@ const getFileMD5 = file => {
const loadNext = () => {
const reader = new FileReader();
reader.onload = e => {
console.log(currentChunkIndex);
progress && progress(Math.round(currentChunkIndex / chunks.length * 100) + "%");
currentChunkIndex++;
spark.append(e.target.result);
if (currentChunkIndex < chunks.length) {
......@@ -58,8 +45,20 @@ const getFileMD5 = file => {
});
};
const getFileChunk = (file, chunkSize) => {
const chunks = [];
const fileSize = file.size;
let currentChunkStart = 0;
while (currentChunkStart < fileSize) {
const chunkEnd = currentChunkStart + chunkSize;
const chunk = file.slice(currentChunkStart, chunkEnd);
chunks.push(chunk);
currentChunkStart = chunkEnd;
}
return chunks;
};
export {
fileMd5Sum,
getFileMD5
getFileMD5Progress
}
......@@ -299,7 +299,7 @@ import {
} from "../../util/http_util";
import { uuid } from "../../util/data_util";
import BigfileUpload from "../../components/common/bigfile_upload.vue";
import {fileMd5Sum, getFileMD5} from '../../util/file_md5.js';
import { fileMd5Sum, getFileMD5Progress } from '../../util/file_md5.js';
export default {
data() {
......@@ -757,21 +757,33 @@ export default {
module: this.module_name
};
//单个文件上传
let load = startLoadingProgress("0%");
let self = this;
this.centerDialogVisible_add = false;
let fd = new FormData();
load.text = "解析中";
getFileMD5(this.form_add.fileList[0].raw)//获取md5
load.text = "";
let md5 = "";
let file = null;
//获取md5
getFileMD5Progress(this.form_add.fileList[0].raw, (progress) => {
load.text = "解析中:" + progress;
}).then(res => {
md5 = res.md5;
file = res.file;
return this.checkFileExist(md5);//检查文件是否存在
})
.then(result => {
fd.append("file", result.file);
fd.append("filename", result.md5);
if (!result) {
let fd = new FormData();
fd.append("file", file);
fd.append("filename", md5);
fd.append("bucket", "MERGE");
return fileUploadProgress("api/rest/file/upload", fd, res => {//文件上传
load.text = "上传中:" + res;
});
} else {
return Promise.reject({ message: "文件已存在" });
}
})
.then(res => {
let fd1 = new FormData();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment