0

I'm using react-native-image-picker (^0.28.0) along with rn-fetch-blob (^0.10.15). It works so far when using a Simulator, but when I use it on real iPhone, the image isn't uploading, it fails to upload with the following error that is being catched by the promise.

"Error: Could not connect to the server."

(Yes, server is up and running)

If you see this, you might think it's a backend issue but other requests work fine on real device, this one is the only having problems. The image request being sent is this one:

{
data: "RNFetchBlob-file:///var/mobile/Containers/Data/Application/843A96A1-0000-40D3-B50F-95D69B94B87A/tmp/5D22283B-2014-4E9E-AC3E-AE677D91A366.jpg"
filename: "IMG_6834.jpg"
name: "pictures"
type: "image/jpeg"
}

The only difference with the simulator device and my iPhone is the data path. Where on simulator is like "RNFetchBlob-file:///Users/marian-mac/Library/Developer/CoreSimulator/Devices/....etc"

Any idea why should be different from the simulator on this? It seems to be sending the same request.

Some extra info, when I preview the image in an Image component, it shows well too. So it seems the path is correct on the real iPhone.

This is the method to upload image

uploadPicture: function(data, token) {
        return RNFetchBlob.fetch('POST', Constants.baseUrl + '/picture', {
            Authorization: "Bearer " + token,
            'Content-Type': 'multipart/form-data;boundary=***BOUNDARY***'
        }, data);
    },

And this is how I build the picture request array, this.state.images contains the image-picker data.

this.state.images.forEach((image) => {
                            reqData.push({
                                data: image.imageFile,
                                filename: image.fileName.split('.')[0] + '.jpg',
                                name: 'pictures',
                                type: image.type
                            })
                        });
msqar
  • 2,940
  • 5
  • 44
  • 90

2 Answers2

0

The problem could be that you are uploading to a non-https url. Had this same issue and the nsAllowArbitraryLoads made it work on simulator and gave error when using it on a real device. Had to wait until it became https

Auticcat
  • 4,359
  • 2
  • 13
  • 28
0

It does not seem to be the problem with http or https. (mine is http)

I'm having a file on this path: filepath =/var/mobile/Containers/Data/Application/F8F06A2A-C04D-4B6D-A316-F879E144366B/Documents/test.wav

I tried RNFetchBlob.wrap(filePath) but no use. So I tried to add this file to the asset of ios project and using RNFetchBlob.wrap(RNFetchBlob.fs.asset('test.wav')). Voila, it works.

As a result, it seems to be the problem with filepath