Load images as both binary strings and blobs.

This commit is contained in:
Jason Miller
2018-03-30 16:26:00 -04:00
parent d023263f57
commit fbcd16063d
2 changed files with 12 additions and 6 deletions

View File

@@ -14,6 +14,7 @@ type Props = {
export type FileObj = { export type FileObj = {
id: number, id: number,
data?: string, data?: string,
uri?: string,
error?: Error | DOMError | String, error?: Error | DOMError | String,
file: File, file: File,
loading: boolean loading: boolean
@@ -25,7 +26,7 @@ type State = {
files: FileObj[] files: FileObj[]
}; };
let counter = 0; let idCounter = 0;
export default class App extends Component<Props, State> { export default class App extends Component<Props, State> {
state: State = { state: State = {
@@ -76,7 +77,7 @@ export default class App extends Component<Props, State> {
@bind @bind
loadFile(file: File) { loadFile(file: File) {
let fileObj: FileObj = { let fileObj: FileObj = {
id: ++counter, id: ++idCounter,
file, file,
error: undefined, error: undefined,
loading: true, loading: true,
@@ -87,11 +88,16 @@ export default class App extends Component<Props, State> {
files: [fileObj] files: [fileObj]
}); });
new Response(file).text() Promise.all([
.then(data => ({ data })) new Response(file).text(),
new Response(file).blob()
])
.then(([data, blob]) => ({
data,
uri: URL.createObjectURL(blob)
}))
.catch(error => ({ error })) .catch(error => ({ error }))
.then(state => { .then(state => {
console.log(state);
let files = this.state.files.slice(); let files = this.state.files.slice();
files[files.indexOf(fileObj)] = Object.assign({}, fileObj, { files[files.indexOf(fileObj)] = Object.assign({}, fileObj, {
loading: false, loading: false,

View File

@@ -28,7 +28,7 @@ export default class Home extends Component<Props, State> {
return ( return (
<div class={style.home + ' ' + (active ? style.active : '')}> <div class={style.home + ' ' + (active ? style.active : '')}>
{ files && files[0] && ( { files && files[0] && (
<img src={files[0].data} /> <img src={files[0].uri} style="width:100%;" />
) } ) }
</div> </div>
); );