From fbcd16063d30c639f03894a51663d32c22813326 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 30 Mar 2018 16:26:00 -0400 Subject: [PATCH] Load images as both binary strings and blobs. --- src/components/app/index.tsx | 16 +++++++++++----- src/components/home/index.tsx | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/components/app/index.tsx b/src/components/app/index.tsx index f958c5d7..2dfd34be 100644 --- a/src/components/app/index.tsx +++ b/src/components/app/index.tsx @@ -14,6 +14,7 @@ type Props = { export type FileObj = { id: number, data?: string, + uri?: string, error?: Error | DOMError | String, file: File, loading: boolean @@ -25,7 +26,7 @@ type State = { files: FileObj[] }; -let counter = 0; +let idCounter = 0; export default class App extends Component { state: State = { @@ -76,7 +77,7 @@ export default class App extends Component { @bind loadFile(file: File) { let fileObj: FileObj = { - id: ++counter, + id: ++idCounter, file, error: undefined, loading: true, @@ -87,11 +88,16 @@ export default class App extends Component { files: [fileObj] }); - new Response(file).text() - .then(data => ({ data })) + Promise.all([ + new Response(file).text(), + new Response(file).blob() + ]) + .then(([data, blob]) => ({ + data, + uri: URL.createObjectURL(blob) + })) .catch(error => ({ error })) .then(state => { - console.log(state); let files = this.state.files.slice(); files[files.indexOf(fileObj)] = Object.assign({}, fileObj, { loading: false, diff --git a/src/components/home/index.tsx b/src/components/home/index.tsx index 1b0ddf0b..ac801711 100644 --- a/src/components/home/index.tsx +++ b/src/components/home/index.tsx @@ -28,7 +28,7 @@ export default class Home extends Component { return (
{ files && files[0] && ( - + ) }
);