Move file from tmp to documents using react-native-fs - react-native

I'm trying to move a file selected from a document picker to the Document Directory using react-native-fs and react-native-document picker.
However, I get the error below:
Error: “file name.mp3” couldn’t be moved to “Documents” because either the former doesn't exist, or the folder containing the latter doesn't exist.
What am I doing wrong?
FYI, I'm using iOS.
openDocumentPicker() {
DocumentPicker.show({
filetype: ['public.audio'],
},(error,url) => {
console.log(url);
this.saveAudio(url);
});
}
saveAudio(url) {
var destPath = RNFS.DocumentDirectoryPath + '/' + 'name';
RNFS.moveFile(url, destPath)
.then((success) => {
console.log('file moved!');
})
.catch((err) => {
console.log("Error: " + err.message);
});
}

I believe I've found the error. The issue was that the file I was uploading had a space in it. I needed to decode the URL first before uploading, like so:
var decodedURL = decodeURIComponent(url)
Then I could move the file over.
RNFS.copyFile(decodedURL, destPath)

It happened to me when destination folder doesn't exist.
[tid:com.facebook.react.JavaScript] 'error!', { [Error: The file “source.jpg” doesn’t exist.]
It's a wrong error message from react-native-fs. It should tell target path folder not exists.

Related

Download txt file from text in React Native

I'm trying to Download txt file from a text but that function is not working fine.
Txt Function
import RNFS from 'react-native-fs';
const txtDownload = () => {
let path = `${RNFS.DocumentDirectoryPath}/${filename}.txt`;
RNFS.writeFile(path, `Here is text`, 'utf8').then((res) => {
Toast('File saved successfully');
}
).catch((err) => {
Toast(err);
});
}
It returns File saved successfully but I can't find file
Log the path and check the location where it's trying to store. For me, it shows the location as below when I try to store in ${RNFS.DocumentDirectoryPath}/help.txt
/data/user/0/com.sample/files/help.txt
I suspect you were checking in the Documents folder. I don't know if you can write to the Documents folder. But it works for the Download folder using RNFS.DownloadDirectoryPath.

Error when moving file with react-native-fs, file already exist

I am trying to move an image to the library using react-native-fs in iOS
const originalPath = "/private/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/tmp/ReactNative/E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png"
const destinationPath = RNFS.LibraryDirectoryPath + "/kj3.jpg";
RNFS.moveFile(screenShotPath, destinationPath).then((data) => {
console.log(data)
}).catch((err) => {
throw err
})
The destination path in this case is
/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/Library/kj3.jpg
I get an error
Error: “E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png” couldn’t be moved to “Library” because an item with the same name already exists.
I also get the same error when I try to copy the file.

Process download file before save

import RNBackgroundDownloader from 'react-native-background-downloader';
let task = RNBackgroundDownloader.download({
id: 'file123',
url: 'https://link-to-very.large/file.zip'
destination: `${RNBackgroundDownloader.directories.documents}/file.zip`
}).begin((expectedBytes) => {
console.log(`Going to download ${expectedBytes} bytes!`);
}).progress((percent) => {
console.log(`Downloaded: ${percent * 100}%`);
}).done(() => {
console.log('Download is done!');
}).error((error) => {
console.log('Download canceled due to error: ', error);
});
// Pause the task
task.pause();
// Resume after pause
task.resume();
// Cancel the task
task.stop();
I am using the above code. In this i have problem that it downloads a file and save it as it. But i want to encode a file then save it in the internal directly of android folder. Let me know if anyone have a idea to do it. So that i can encode after that file will save.
Thanks
How about you download the file and then encode the local file and then save the encoded file and delete the original file.

How to delete uploaded file in Sailsjs (req.file)?

In Sails.js, one could receive an uploaded file as such:
myControllerAction: function(req, res) {
req.file('avatar', function(err, uploadedFiles) {
// uploaded avatar image will be available here
console.log(uploadedFiles[0]);
}
}
Suppose I received a file, but it is not properly formatted the way I want. I would just reply with an error. One thing I would like to do is make sure that received file does not remain in the filesystem (i.e. if it exists somewhere, delete it). How can I ensure that?
Just use node fs module to delete uploaded file.
const fs = require('fs');
fs.unlink(insertFilePathHere, function(err) {
if (err) return console.log(err); // handle error as you wish
// file deleted... continue your logic
});

How to change mp3 file to wav file in node.js

I am trying to convert mp3 file to wav file but I am not getting idea how to do that, I tried using fluent-ffmpeg library but I don't know how to use that.
I finally figured it out using 'fluent-ffmpeg' library. Here is my code.
const ffmpeg = require('fluent-ffmpeg');
let track = './source.mp3';//your path to source file
ffmpeg(track)
.toFormat('wav')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
})
.save('./hello.wav');//path where you want to save your file
if you are facing
An error occurred: Cannot find ffmpeg
then add the ffmpeg path in system environment variables. Your VSCode still may dont recognise the ffmpeg command so in that case re-start VSCode.