Skip to content

Instantly share code, notes, and snippets.

@akirco
Last active May 19, 2023 05:46
Show Gist options
  • Save akirco/869cbcd06646c6b10beccc409379b6de to your computer and use it in GitHub Desktop.
Save akirco/869cbcd06646c6b10beccc409379b6de to your computer and use it in GitHub Desktop.
file upload
/**
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