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

单文件上传的优化

parent 16fc2df3
...@@ -19,21 +19,8 @@ var fileMd5Sum = (file) => { ...@@ -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) => { return new Promise((resolve, reject) => {
const chunkSize = 2097152; // 每次读取2MB. const chunkSize = 2097152; // 每次读取2MB.
const chunks = getFileChunk(file, chunkSize); const chunks = getFileChunk(file, chunkSize);
...@@ -42,7 +29,7 @@ const getFileMD5 = file => { ...@@ -42,7 +29,7 @@ const getFileMD5 = file => {
const loadNext = () => { const loadNext = () => {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = e => { reader.onload = e => {
console.log(currentChunkIndex); progress && progress(Math.round(currentChunkIndex / chunks.length * 100) + "%");
currentChunkIndex++; currentChunkIndex++;
spark.append(e.target.result); spark.append(e.target.result);
if (currentChunkIndex < chunks.length) { if (currentChunkIndex < chunks.length) {
...@@ -58,8 +45,20 @@ const getFileMD5 = file => { ...@@ -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 { export {
fileMd5Sum, fileMd5Sum,
getFileMD5 getFileMD5Progress
} }
...@@ -299,7 +299,7 @@ import { ...@@ -299,7 +299,7 @@ import {
} from "../../util/http_util"; } from "../../util/http_util";
import { uuid } from "../../util/data_util"; import { uuid } from "../../util/data_util";
import BigfileUpload from "../../components/common/bigfile_upload.vue"; 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 { export default {
data() { data() {
...@@ -757,21 +757,33 @@ export default { ...@@ -757,21 +757,33 @@ export default {
module: this.module_name module: this.module_name
}; };
//单个文件上传
let load = startLoadingProgress("0%"); let load = startLoadingProgress("0%");
let self = this; let self = this;
this.centerDialogVisible_add = false; this.centerDialogVisible_add = false;
load.text = "";
let fd = new FormData(); let md5 = "";
load.text = "解析中"; let file = null;
getFileMD5(this.form_add.fileList[0].raw)//获取md5 //获取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 => { .then(result => {
fd.append("file", result.file); if (!result) {
fd.append("filename", result.md5); let fd = new FormData();
fd.append("bucket", "MERGE"); fd.append("file", file);
return fileUploadProgress("api/rest/file/upload", fd, res => {//文件上传 fd.append("filename", md5);
load.text = "上传中:" + res; fd.append("bucket", "MERGE");
}); return fileUploadProgress("api/rest/file/upload", fd, res => {//文件上传
load.text = "上传中:" + res;
});
} else {
return Promise.reject({ message: "文件已存在" });
}
}) })
.then(res => { .then(res => {
let fd1 = new FormData(); 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