React Native get real path of file on Android - javascript

I'm using react-native-document-picker to select a file and react-native-fs to upload the file.
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
copyToCacheDirectory: false
});
I'm getting the uri as - content://com.android.providers.media.documents/document/image%3A24
Then I'm converting this to actual path using react-native-get-real-path.
RNGRP.getRealPathFromURI(res[0].uri)
.then(filePath =>{
realFilePath = filePath
console.log("real-path-->"+filePath)
ToastAndroid.show(filePath, ToastAndroid.SHORT);
})
The above method gives the path as /storage/emulated/0/Download/output-onlinepngtools.png but this working for few files, for others I'm getting null as the real-path.
This is the error I see while uploading the file - [Error: Socket is closed]

Related

Trying to use AXIOS for get request, error "require is not defined" [duplicate]

This question already has answers here:
Client on Node.js: Uncaught ReferenceError: require is not defined
(11 answers)
Closed 11 months ago.
My code is below. This is my first time using NodeJS and axios.
I have installed NodeJS on Windows using the downloadable installer. I then installed Axios using npm install axios in powershell as admin.
I'm obviously copy/pasting the boilerplate nodejs code for an axios get request.
I've tested the php file using a curl get request from command line (installed actual curl, not the windows shortcut to another program.) So the PHP is accepting get requests and serving json back properly.
The issue I'm stuck on is how to setup axios and node js properly so that my js file runs the get request properly.
This is my code
const loginForm = document.getElementById("login-form");
const loginButton = document.getElementById("login-form-submit");
const loginErrorMsg = document.getElementById("login-error-msg");
const vanillaUrl = "localhost:8081/hospital/php/addapt.php";
const getUrl = "localhost:8081/hospital/php/getpatientappt.php?PatientID=1";
const axios = require('axios')
//get request to get appt for patient id1
axios
.get(geturl)
.then(res => {
console.log(`statusCode: ${res.status}`)
console.log(res)
alert(res)
})
.catch(error => {
console.error(error)
})
loginButton.addEventListener("click", (e) => {
e.preventDefault();
const username = loginForm.username.value;
const password = loginForm.password.value;
if (username === "user" && password === "1") {
alert("You logged in!");
location.reload();
} else {
loginErrorMsg.style.opacity = 1;
}
}
)
These are the errors I'm getting in chrome from the javascript console
Uncaught ReferenceError: require is not defined
at login-page.js:7:15
DevTools failed to load source map: Could not load content for chrome-extension://hnfanknocfeofbddgcijnmhnfnkdnaad/content.js.map: System error: net::ERR_BLOCKED_BY_CLIENT
DevTools failed to load source map: Could not load content for http://localhost:8081/hospital/login/requestProvider.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
When I run incognito I get
Uncaught ReferenceError: require is not defined
at login-page.js:7:15
there is no file requestProvider.js.map or content.js.map
This is all being hosted on localhost with xampp. I'm not sure if that affects anything.
I'm not sure what this error is. Got stuck googling it. And I'm not sure how to integrate this code properly.
seems like you're running this code in browser, browser implementation of Javascript doesn't have require function

error: (-215) !empty() in function detectMultiScale in Opencv.JS

I have checked and tried these solutions here but none is helping
error: (-215) !empty() in function detectMultiScale
OpenCV.js - detectMultiScale "This Exception cannot be caught"
I also read elsewhere that the problem could be because the file needs to be served via an Http request and so I decided to use ngrok but I still found the same error. Currently am using python http.server
Here is the section of the code:
let classifier = new cv.CascadeClassifier();
let utils = new Utils('errorMessage'); //use utils class
let faceCascadeFile = "http://localhost:8000/docs/haarcascade_frontalface_default.xml";
//let faceCascadeFile = "D:\Code\Fit-to-Interact-web\docs\haarcascade_frontalface_default.xml"
//let faceCascadeFile = "https://0979da552cb5.ngrok.io/docs/haarcascade_frontalface_default.xml"; // path to xml
// use createFileFromUrl to "pre-build" the xml
//Make sure to run it in a webserver because it has to do XMLHTTPRequests.
utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
classifier.load(faceCascadeFile); // in the callback, load the cascade from file
});
console.log(classifier.empty())
Worse when I use the absolute/relative file path I get this error Not allowed to load local resource or sometimes the same error:
Exception: OpenCV(4.4.0) /build/master-contrib_docs-lin64/opencv/modules/objdetect/src/cascadedetect.cpp:1689:
error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

How to pass String in to fs.readFileSync

I'm trying to convert string in to Buffer using readFileSync.It's return buffer with hard coded string.but not when i pas string.
----working ---
const buffer = fs.readFileSync('./test/JFwZCrdEojAr09ajT8EPZmo.jpg',{ encoding: 'utf8' });
----not working ---
var pathString = './test/JFwZCrdEojAr09ajT8EPZmo.jpg';
const buffer = fs.readFileSync(pathString,{ encoding: 'utf8' });
i'm getting bellow error
Error: ENOENT: no such file or directory, open
'./test/JFwZCrdEojAr09ajT8EPZmo.jpg'
Both ways working fine in local machine.but when deploy in to server,above happening.
Both should be work. check your image file still exists.

Creating a new file inside a path in nodejs

I have a path in the file system, say
/home/user/lannister/wines/red/knowthings
Inside lannister folder, my nodejs code resides. What the code is supposed to do, is, to create a blank file called debtslist.txt which should be located inside knowthings folder, so that the file path becomes
/home/user/lannister/wines/red/knowthings/debtslist.txt
I tired several ways of doing it, that include:
const filePath = 'wines/red/knowthings/debtslist.txt';
fs.openSync(filePath, 'w');
and
const filePath = 'wines/red/knowthings/debtslist.txt';
fs.writeFile(filePath, '', function (err) {
if (err) throw err;
});
But every time I am getting this error:
Error
Error: ENOENT: no such file or directory, open 'wines/red/knowthings/debtslist.txt'
Am I specifying the path in const filePath in a wrong way?

React-native Invalid UTF-8 detected while trying to read a image file

I am trying to read an image file using the URI using react-native-fs and redux-saga:
file:///var/mobile/Containers/Data/Application/605FB6C0-869C-4128-883E-A59616933C64/Documents/images/52108C66-A087-4942-9DD4-22CBD0327089.jpg
Below is the line where I am getting an error while trying to read the image file:
const imageFile = yield call([RNFS, RNFS.readFile], logo.uri);
Below is the error I am getting:
Error: Invalid UTF-8 detected
at decodeSymbol (utf8.js:194)
at Object.utf8decode [as decode] (utf8.js:206)
at FS.common.js:150
at tryCallOne (core.js:37)
at core.js:123
at JSTimers.js:301
at _callTimer (JSTimers.js:154)
at _callImmediatesPass (JSTimers.js:202)
at Object.callImmediates (JSTimers.js:470)
at MessageQueue.__callImmediates (MessageQueue.js:275)
at MessageQueue.js:140
Can someone please tell me what I am doing wrong?
This is how it worked for me with a JPEG file on iOS :
(...)
var RNFS = require('react-native-fs');
(...)
var uri = '{your file uri}'
var img = 'file:///' + (uri.replace('file://', '')); // Must do that for RNFS
RNFS.readFile(img, 'base64') // 'base64' to process binary format
.then((file) => {
console.log("Getting image");
console.log(file);
})
RNFS only read file as string, the default encoding is utf8. image is binary file, you should try some other library to read it

Categories

Resources