AnimationTool/app/file_handler.ts

34 lines
914 B
TypeScript
Raw Normal View History

2019-09-25 03:16:07 +00:00
export class FileHandler {
public static ProcessImages = (fileList: FileList): Promise<[string[], string[]]> => {
2019-09-26 01:59:16 +00:00
return new Promise(async (resolve, reject) => {
const processedFilenames: string[] = [];
const originalFilenames: string[] = [];
2019-09-25 03:16:07 +00:00
2019-09-26 01:59:16 +00:00
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
const filename = await FileHandler.ProcessImage(file);
processedFilenames.push(filename);
originalFilenames.push(file.name);
2019-09-26 01:59:16 +00:00
}
resolve([ processedFilenames, originalFilenames ]);
2019-09-26 01:59:16 +00:00
});
};
private static ProcessImage = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
2019-09-25 03:16:07 +00:00
const reader = new FileReader();
2019-09-28 02:29:42 +00:00
reader.onload = (event: ProgressEvent<FileReader>) => {
resolve(event.target!.result as string);
2019-09-26 01:59:16 +00:00
};
2019-09-25 03:16:07 +00:00
2019-09-26 01:59:16 +00:00
reader.onerror = reject;
2019-09-25 03:16:07 +00:00
2019-09-26 01:59:16 +00:00
// Read in the image file as a data URL.
reader.readAsDataURL(file);
});
};
2019-09-25 03:16:07 +00:00
}