Fetching fileReader blocks.

I use the fetch API to send a base64 photo for my backend for manipulation, and then return it. I want to read a photograph and write it to my camera, which I can take. The ultimate goal is to share the photo with instagram, but for some reason, the Reader file stops the props from updating. Here is a way:

uploadImageAsync = async (uri, setImageText) => {
    let apiUrl = "some url"

    let uriParts = uri.split('.');
    let fileType = uri[uri.length - 1];

    let formData = new FormData();
    formData.append('photo', {
        uri,
        name: `photo`,
        type: `image/${fileType}`,
    });
    let options = {
        method: 'POST',
        body: formData,
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'multipart/form-data',
        },
    };
    return fetch(apiUrl, options)
        .then(response => {
            return response.blob();
        })
        .then( async myBlob => {
            const fileReader = new FileReader();
            fileReader.readAsText(myBlob);
            const objectURL = URL.createObjectURL(myBlob);
            fileReader.addEventListener('loadend', async (e) => {
                const text = e.srcElement.result;
                this.props.setImageText(text);
                let source = { uri: 'data:image/jpeg;base64,' + text };
                const cameraRollAddress = await CameraRoll.saveToCameraRoll(source.uri, 'photo');
                let instagramURL = `instagram://library?AssetPath=${cameraRollAddress}`;
                Linking.openURL(instagramURL);
                return;
            });
        });
}

The photo is saved in the camera roll, but the props are not updated, so it Linkingdoes not happen until I press another button somewhere on the screen.

, , Instagram , . , , - , : fileReader, . , , , base64 .

, base64 ( Redux), .

+4
1

, uri , :

Redux , React ?

- Redux. :

let uriParts = uri.split('.');

, uri "addEventListener".

, uri , .

, !

+2

Source: https://habr.com/ru/post/1690029/


All Articles