我想从我的react-native应用程序上的socket.io服务器上传视频, 从html-js客户端,上传工作完美。
当我尝试使用react-native执行此操作时,文件将被发送,但在服务器上找到文本,之后我无法读取它。
捕获后保存的视频文件在手机图库中清晰易读。
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
}
这是编码问题吗?如何用缓冲区读取文件?
谢谢你, 巴斯蒂安
答案 0 :(得分:0)
编码(缓冲区) - 将ArrayBuffer编码为base64字符串
decode(str) - 将base64字符串解码为ArrayBuffer
使用此npm包