I want to upload a video on my socket.io server from my react-native application , From an html-js client, the upload works perfectly.
When I try to do it with react-native, the file is sent but is found as text on the server and I can not read it afterwards.
The video file saved after the capture is clearly legible in the phone gallery.
import Camera from "react-native-camera";
import SocketIOClient from 'socket.io-client';
import RNFS from 'react-native-fs';
export default class CaptureVideoScreen extends React.Component {
constructor(props) {
super(props);
this.socketInit();
this.camera = null;
this.file = null;
this.state = {
camera: {
aspect: Camera.constants.Aspect.fill,
captureTarget: Camera.constants.CaptureTarget.cameraRoll,
type: Camera.constants.Type.back,
captureQuality: "720p",
flashMode: Camera.constants.FlashMode.auto,
},
isRecording: false
};
}
socketInit = () => {
this.socket = SocketIOClient('https://mywebsite.fr:3001');
this.socket.on('Upload.RequestData', this.emitData);
this.socket.on('Upload.Done', this.uploadDone);
};
startRecording = () => {
if (this.camera && !this.state.isRecording) {
this.camera.capture({mode: Camera.constants.CaptureMode.video})
.then((data) => {
RNFS.readFile(data.path, 'base64').then(contents => {
this.file = contents;
this.socket.emit('Upload.Start', {'size': contents.length});
});
})
.catch(err => console.error(err));
setTimeout(this.stopRecording, 12000);
this.setState({
isRecording: true
});
}
};
stopRecording = () => {
if (this.camera && this.state.isRecording) {
this.camera.stopCapture();
this.setState({
isRecording: false
});
}
};
emitData = (data) => {
var buffer = this.file.slice(data.cursor, data.cursor + data.blockSize);
console.log(buffer.length);
this.socket.emit('Upload.Data', {token: data.token, data: buffer});
console.log(Math.round(data.cursor / this.file.length * 100) + '%');
};
uploadDone = (data) => {
console.log('Upload Done !');
};
//... render + camera methods
}
Is it an encoding problem? How can I read the file with a buffer?
Thank you, Bastien