Last active
August 7, 2022 02:19
-
-
Save semlinker/4c04368541146a8083c9fd6a138227d1 to your computer and use it in GitHub Desktop.
Concurrent Upload of Large Files in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function uploadFile() { | |
if (!uploadFileEle.files.length) return; | |
const file = uploadFileEle.files[0]; | |
const fileMd5 = await calcFileMD5(file); // Calculate the MD5 of the file | |
const fileStatus = await checkFileExist( | |
// Check if the file already exists | |
"/exists", | |
file.name, | |
fileMd5 | |
); | |
if (fileStatus.data && fileStatus.data.isExists) { | |
alert("File has been uploaded"); | |
return; | |
} else { | |
await upload({ | |
url: "/single", | |
file, | |
fileMd5, | |
fileSize: file.size, | |
chunkSize: 1 * 1024 * 1024, | |
chunkIds: fileStatus.data.chunkIds, | |
poolLimit: 3, | |
}); | |
} | |
const fileData = await concatFiles("/concatFiles", file.name, fileMd5); | |
const { | |
data: { | |
data: { url }, | |
}, | |
} = fileData; | |
alert(`Uploaded file url is: ${url}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment