react-native-fs
react-native-fs copied to clipboard
Reading ByteArray (raw bytes) directly from file
Hi, I'm uploading media files (images, wav files) from my phone to S3 bucket. To upload, I'll need the Byte array. Currently I'm reading in base64, and then decode it back to byteArray and then finally uploading it.
Currently, I'm doing the following,
let base64Data = await RNFS.readFile(fPath, 'base64');
const arrayBuffer = AWS.util.base64.decode(base64Data)
I'm trying to find a way to avoid the base64.decode() operation.
The Documentation says,
encoding can be one of utf8 (default), ascii, base64. Use base64 for reading binary files.
Question: Is there a way to directly read raw bytes from the file?
Thank you.
I find that using react-native-quick-base64 to decode base64 is fast enough. I am using the code below to convert buffer into String or WritableArray in native and then get the String or Array in javascript. But it's slower than using base64.
@ReactMethod
public void readRaw(String filepath, int length, int position, Promise promise) {
try {
InputStream inputStream = getInputStream(filepath);
byte[] buffer = new byte[length];
inputStream.skip(position);
int bytesRead = inputStream.read(buffer, 0, length);
// // toString
// StringBuilder resultBuilder = new StringBuilder(buffer.length);
// for (int i = 0; i < bytesRead; i++) {
// resultBuilder.append((char) buffer[i]);
// }
// promise.resolve(resultBuilder.toString());
// to Array
WritableArray resultArr = Arguments.createArray();
for (int i = 0; i < bytesRead; i++) {
resultArr.pushInt((int) buffer[i]);
}
promise.resolve(resultArr);
} catch (Exception ex) {
ex.printStackTrace();
reject(promise, filepath, ex);
}
}
Thanks @foxnes for your inputs. We are uploading files over 500MB. I believe totally avoiding base64 encode/encode for 500MB is the right approach.
Thanks @foxnes for your inputs. We are uploading files over 500MB. I believe totally avoiding base64 encode/encode for 500MB is the right approach.
Hi, have you found a solution?