react-native-firestack icon indicating copy to clipboard operation
react-native-firestack copied to clipboard

filepath.replace is not a function

Open justfede opened this issue 7 years ago • 15 comments

I got "filepath.replace is not a function" when I try to upload some picture to firebase storage.

`
const firestack = new Firestack();

    firestack.storage.setStorageUrl('chatapp-e4bc6.appspot.com');
    firestack.storage.uploadFile('photos/1.jpg', source, {
      contentType: 'image/jpeg',
      contentEncoding: 'base64',
    })
    .then((res) => console.log('The file has been uploaded'))
    .catch(err => console.error('There was an error uploading the file', err))`

Running on Android Emulator.

err

update: The problem is in "storage.js" line 85:

justfede avatar Feb 08 '17 22:02 justfede

Want to make a PR?

auser avatar Feb 12 '17 18:02 auser

Got the same problem. How did you fix it?

Jorrit2002 avatar Feb 23 '17 19:02 Jorrit2002

@justfede @Jorrit2002 Just had the same issue. Turned out I was passing the whole image object instead of image.path, a string. Make sure source is a path string, not an object.

jacenko avatar Mar 16 '17 13:03 jacenko

Right @jacenko . That's was my error!

justfede avatar Mar 19 '17 00:03 justfede

@justfede I have a similar issue, but I couldn't get it solved. You could check the code below.

  import RNFirebase from 'react-native-firebase';
  import RNFetchBlob from 'react-native-fetch-blob';
  
  // Prepare Blob support
  const Blob = RNFetchBlob.polyfill.Blob;
  const fs = RNFetchBlob.fs;
  
  export const firebaseApp = RNFirebase.initializeApp(configurationOptions);
  
  ...
  
  let uploadBlob = null;
  const imageRef = firebaseApp.storage().ref('/photos/`).child('image');

  fs.readFile(uploadUri, 'base64')
    .then((data) => {
      return Blob.build(data, { type: `${mime};BASE64` });
    })
    .then((blob) => {
      uploadBlob = blob;
      return imageRef.put(blob, { contentType: mime });
    })
    .then(() => {
      uploadBlob.close();
      return imageRef.getDownloadURL();
    })
    .then((url) => {
      //Success
      console.log('success');
    })
    .catch((error) => {
      console.log('failed on uploading image:', error);
  })
  ...

I get this error. { [TypeError: undefined is not a function (evaluating 'filePath.replace('file://', '')')]

Can you please help me?

danielfred1129 avatar Aug 14 '17 18:08 danielfred1129

Got this error

TypeError: filePath.replace is not a function at StorageReference.putFile

Code

when trying to upload a blob (not base64) like so:

import RNFetchBlob from 'react-native-fetch-blob';

...

// Prepare Blob support
const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

...

// Upload function
Blob
  .build(RNFetchBlob.wrap(image.path), { type : 'image/jpeg' })
  .then((blob) => firebase.storage()
    .ref('avatars').child('myimage.jpg')
    .put(blob, { contentType : 'image/jpg' })
    .then((snapshot) => {
      resolve(snapshot);
    })
  )
  .catch((error) => {
    reject(error);
  });

Versions

  "dependencies": {
    "react": "16.0.0-alpha.12",
    "react-native": "0.48.4",
    "react-native-fetch-blob": "^0.10.8",
    "react-native-image-crop-picker": "^0.17.2",
    "react-redux-firebase": "^1.5.0",
  },

Does anyone succeeded to send an image/blob/file? Thank

MaxInMoon avatar Oct 06 '17 10:10 MaxInMoon

Same error here, any suggestions?

Edit: return imageRef.put(blob._ref, { contentType: mime });

Fixed it for me, but files are getting uploaded without file extension, any ideas?

RobinKuiper avatar Oct 19 '17 00:10 RobinKuiper

+1

perrosnk avatar Nov 09 '17 20:11 perrosnk

I think I found the solution.. If you are using react-native-firebase, you don't need to create a blob file. Just pass the path of the picture to .put method. imageRef.put(uri, { contentType: mime }); It seems react-native-firebase takes care of all else.

perrosnk avatar Nov 09 '17 22:11 perrosnk

@perrosnk Thanks! I can solve the same problem by this!

atyenoria avatar Nov 23 '17 12:11 atyenoria

@perrosnk Thanks I spent whole day for this error But now it works for me

very very thanks

kaushalmarakana avatar Dec 10 '17 12:12 kaushalmarakana

Thanks @pedro-ribeiro . It solved my issue.

andyngdz avatar May 23 '18 06:05 andyngdz

Before I used rn-fetch-blob with 'firebase' (the npm package), when I use react-native-firebase, no need this lib (rn-fetch-blob) anymore. imageRef.put(uri,... like @perrosnk mentioned is a good answer for the issue.

khuongdv avatar Aug 04 '18 17:08 khuongdv

Guys can anyone plz paste the complete code you used to get this working. I m using "react-native-firebase" . I am looking for the solution prescribed by @perrosnk , to be specific. thank you.

@kaushalmarakana, can I hv your email id bro???

nikhilmeria avatar Jan 13 '19 13:01 nikhilmeria

I think I found the solution.. If you are using react-native-firebase, you don't need to create a blob file. Just pass the path of the picture to .put method. imageRef.put(uri, { contentType: mime }); It seems react-native-firebase takes care of all else.

can you show me the full codes pleasee

tanthanhtran2708 avatar Sep 26 '19 04:09 tanthanhtran2708