Node.js '[object File]' BLOB - javascript

I'm trying to obtain a BLOB from a request. The request object is constructed using FormData in Angular.
const buffer = fs.readFileSync(fileFromRequest);
The code above returns an error:
Error: ENOENT: no such file or directory, open '[object File]'
I can't find any resource to read/parse [object File].
Hope you guys can help me on this. Many thanks!

When upload a file using angular to node.js you cant use this
const buffer = fs.readFileSync(fileFromRequest);
The fs module only use to read to local file from the server. To handle upload data you can use multer

Related

How to obtain a relative path from device filesystem path?

I’m looking to allow a user to upload a javascript file containing a thread and then to spawn that thread, but I’m facing some issues with the device paths.
First I use the DocumentPicker to pick the file:
const pickerResult = await DocumentPicker.pickSingle({
type: [types.allFiles],
copyTo: 'documentDirectory'
});
Then I use the RNFS.readDir to get the file and its path comes back as /data/user/0/playground.com/files/my-thread.js.
Now, the react-native-threads expects a relative path like such new Thread('./worker.thread.js’);. If I try to pass the path received by RNFS I receive the following no such file error:
Error: ENOENT: no such file or directory, open '/data/user/0/playground.com/files/my-thread.js'.
I feel like this may be possible to avoid by using a Blob, but perhaps there is an easier way?

Firebase admin when uploading files to storage through url: Error: ENOENT: no such file or directory

So I am trying to make a music app where people can upload music there. First, the client takes file and changes it to object url like this:
const track_src = URL.createObjectURL(track);
data.track_src = track_src;
await Req.post("/api/u/music/upload", data)
After that, the server receives the data and object url and uploads it to Firebase storage:
//track_src is the object url
await st.bucket(sid).upload(track_src, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
})
But I get error that says:
Error: ENOENT: no such file or directory, stat 'E:\Server\blob:http:\localhost:3000\91e53bb5-abf2-46b4-bd0c-268b242e93f3'
What you are trying to do is not possible. There are basically 2 methods of uploading files in Storage, you either have to :
Use the bucket().upload() method to upload, which accepts the path to your local file as the pathString parameter, so you need the actual file for this, not a url. This is the ideal option if you have the file stored locally and given the information you shared this might be the way to go for you. You can look at this answer for more information.
Use the bucket().file() to create an empty file in Storage and then use the file.createWriteStream() method to get a stream that can write to the file content. This can be a valid solution if you have the file in memory.
I would suggest you to take a look at this documentation for the methods offered for the bucket class and this documentation for the methods offered for the file class.

fs.createReadStream is not finding file

I'm studying Node.js right now, using the "Beginning Node.js" textbook.
The example in the book does not execute properly in the command prompt. (I'm using Ubuntu 18.04 with Node v9.4.0, and the book is three years old, so this may be related?)
In the downloadable source code for the book, this is the code that is provided:
var fs = require('fs');
// Create readable stream
var readableStream = fs.createReadStream('./cool.txt');
// Pipe it to out stdout
readableStream.pipe(process.stdout);
The file, cool.txt, is in the parent directory.
--parentFolder
----jsFileToExecute.js
----cool.txt
When I run node jsFileToExecute.js in the command prompt, I get this response:
events.js:137
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open './cool.txt'
As this source code is directly from the textbook publisher's website and it still doesn't run, I'm assuming there's something wrong with it?
Looking for solutions to figure out why this isn't working. I've looked at the documentation at nodejs.org, but didn't find any clues.
Thank you for any help!
To avoid paths issues, it's recommended to use path.join, like:
const path = require('path');
const coolPath = path.join(__dirname, 'cool.txt');
const readableStream = fs.createReadStream(coolPath);
With above example, you're creating a path to the file referring to the current directory of the script stored in global variable called __dirname.
if the file is in the same directory where currently you are running your project then you can simply type the name of the file instead of giving it the path like ./ or / like here:
var fs = require('fs');
// Create readable stream
var readableStream = fs.createReadStream('cool.txt');
// Pipe it to out stdout
readableStream.pipe(process.stdout);
It will work as will read the file from the same directory

Absolute path using fs in nodejs

I am trying to pipe an http audio stream from my nodejs server:
var streamPath = 'http://127.0.0.1:1485/mystream.mp3';
var stat = fs.statSync(streamPath);
response.writeHead(200, {'Content-Type': 'audio/mpeg','Content-Length': stat.size});
fs.createReadStream(streamPath).pipe(response);
The problem is that fs doesn't like the absolute path and I get the following error:
Error: ENOENT: no such file or directory, stat 'C:\myserver\http:\127.0.0.1:1485\mystream.mp3'
I can't find a way to use the absolute path. Is this even possible using fs?
'http://127.0.0.1:1485/mystream.mp3' is not an absolute path, it's a URL.
Absolute paths are something like /home/x/dir/file.txt on your filesystem.
If you want to get a stream from a URL then you need to use http or request. Or you need to use a path on your filesystem and not a URL if you want to get a file on your filesystem without using the network.
fs.createReadStream can only open local file in your filesystem.
For more details, see:
https://nodejs.org/api/fs.html
To get a file over a network, see:
https://www.npmjs.com/package/request
https://www.npmjs.com/package/request-promise
You are trying to access a remote resource through fs - that is wrong. fs is meant to be used for the Local Filesystem. If you want to access any remote resource you have to use http or https. However if I see things correctly you are trying to access your localhost, which should work.
Your Application is trying to access following file: C:\myserver\http:\127.0.0.1:1485\mystream.mp3 if you look closely that can't work. Your Path is mixed with your local path and a remote source (which is localhost actually). Try to fix your path, that should solve your problem. Keep in mind that fs will only work on your local system.
You should also think about fs.statSync this will block everything else until its finished.
Docs:
fs: https://nodejs.org/api/fs.html
http: https://nodejs.org/api/http.html
https: https://nodejs.org/api/https.html
Regards,
Megajin

Uploading image from React-Native app to Firebase storage

In React-Native app how can i upload an image from user' device to Firebase storage? I'm doing like:
var file = {
uri: 'file:///Users/...../LONG_PATH/....../Documents/images/name.jpg'
}
var storageRef = this.firebase.storage().ref();
var uploadTask = storageRef.child('images/' + file.name).put(file);
But it throws an error:
Possible Unhandled Promise Rejection (id: 0):
Firebase Storage:
Invalid argument in put at index 0: Can't find variable: Blob
In this post, software engineer said:
React Native does not support the File and Blob types, so Firebase Storage uploads will not work in this environment. File downloads do work however.
But I found this module wich can transform file to blob types. Maibe can help in your solution
I found this, hopefully this helps. Functional React-Native with Firebase storage Example

Categories

Resources