-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix conflicting File name and Promise resolve issue
- Loading branch information
Showing
4 changed files
with
42 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,41 @@ | ||
import { ref } from "vue"; | ||
import { ref } from 'vue' | ||
|
||
export default function () { | ||
const files = ref<File[]>([]) | ||
const serializeFile = (file: File) => { | ||
const reader = new FileReader() | ||
reader.onload = (e: any) => { | ||
files.value.push({ | ||
...file, | ||
name: file.name, | ||
size: file.size, | ||
type: file.type, | ||
lastModified: file.lastModified, | ||
content: e.target.result, | ||
}) | ||
} | ||
reader.readAsDataURL(file) | ||
} | ||
|
||
const handleFileInput = (event: any): Promise<void> => { | ||
const files = ref<ClientFile[]>([]) | ||
const serializeFile = (file: ClientFile): Promise<void> => { | ||
return new Promise<void>((resolve, reject) => { | ||
files.value.splice(0) | ||
// console.log('handleFileInput event: ' + event) | ||
|
||
const promises = [] | ||
for (const file of event.target.files) { | ||
promises.push(serializeFile(file)) | ||
const reader = new FileReader() | ||
reader.onload = (e: ProgressEvent<FileReader>) => { | ||
files.value.push({ | ||
...file, | ||
name: file.name, | ||
size: file.size, | ||
type: file.type, | ||
lastModified: file.lastModified, | ||
content: e.target?.result, | ||
}) | ||
resolve() | ||
} | ||
|
||
Promise.all(promises) | ||
.then(() => resolve()) | ||
.catch((error) => reject(error)) | ||
reader.onerror = (error) => { | ||
reject(error) | ||
} | ||
reader.readAsDataURL(file) | ||
}) | ||
} | ||
|
||
const handleFileInput = async (event: any) => { | ||
files.value.splice(0) | ||
|
||
const promises = [] | ||
for (const file of event.target.files) { | ||
promises.push(serializeFile(file)) | ||
} | ||
|
||
await Promise.all(promises) | ||
} | ||
|
||
return { | ||
files, | ||
handleFileInput, | ||
} | ||
} | ||
|
||
interface File extends Blob { | ||
content: any | ||
name: string | ||
lastModified: string | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
interface ServerFile { | ||
name: string | ||
content: string | ||
size: string | ||
type: string | ||
lastModified: string | ||
} | ||
|
||
interface ClientFile extends Blob { | ||
content: string | ArrayBuffer | ||
name: string | ||
lastModified: number | ||
} |