Last active
May 19, 2023 05:46
-
-
Save akirco/869cbcd06646c6b10beccc409379b6de to your computer and use it in GitHub Desktop.
file upload
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 (e: Event) => { | |
const chunkSize = 2 * 1024 * 1024; | |
const target = e.target as HTMLInputElement; | |
const targetFile = target.files![0]; | |
const { type, name, size } = targetFile; | |
checkType(type); | |
let [index, start] = [0, 0]; | |
while (start < size) { | |
let blob = null; | |
if (start + chunkSize < size) { | |
blob = targetFile.slice(start, size); | |
} else { | |
blob = targetFile.slice(start, start + chunkSize); | |
} | |
start += chunkSize; | |
const blobFile = new File([blob], name); | |
const formData = new FormData(); | |
formData.append("file", blobFile); | |
formData.append("index", index + ""); | |
await request.upload("/api/upload", formData); | |
index++; | |
state.percent = (start / size) * 100; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment