How to download file from mongo and store in a folder - node.js

I have created an MEAN stack web application which uploads image file and store in mongo database. I used Mongo GridFs to store files in Mongo database. it is working fine.
Now I have requirement to create a job using Nodejs to download those images in a folder. I am using "openDownloadStream" method of grid fs to get the file stream. How to store this stream as file in a folder.
There is no http call as this is a simple function which will run as a batch.

Related

Where do I store uploaded images or files in a mern stack app

I'm working on a project. I'm using node and express for the rest api and next js for the front-end.
I have form where users can add new products.
Where should I store the uploaded images?
Should I create a folder uploads in the website folder root or upload images in client/public/assets/img/?
If I create a folder uploads in root how should I access those images in the front-end?
I know how to upload images or files. I just want to know what is best location to store them
This my project structure
Website
--------
client
-------
components
pages
public
styles
...
server
------
config
controllers
middlewares
models
routes
utils
server.js
You have to be create a Upload folder in backend directory. When client upload an image you have move that image in Upload folder with specific name and that name should save in database.
Please check this one uploadFile
Alternatively, you can use a storage service like AWS S3 to store files as at some point, your server's storage will be full.
To implement you can use AWS's js SDK with multer and multer s3 uploader to get it working.

How to download a file from server in react?

I have a Node.js backend that sends an icon image to a React frontend. Initially, I encode this image as a Base64 string and store it directly in the database. I created an api that returns this string to the frontend. However, I read that it is bad to store it in the database, so I re-wrote the api to store this string in the server as a json file and now my api returns the absolute path to this json file. However, how is the React frontend supposed to retrieve the file? Is it possible to use the absolute path or do I have to create another api to return a file object ? If so, how do I do that?
I'm a newbie at both react and node.js so any help is appreciated.Thanks.
EDIT: I stored the file path as __dirname of where my code resides + filename.I'm getting cors error when accessing that url
Use Express to share out that folder as such-and-such.
Suppose you keep your files in a folder called files, just off the root. And suppose your server structure looks roughly like this:
- backend
- app.js
- frontend
- Components
- Routes
- public
- dev
- index.html (your template file)
- files
- myimage.png
In your root js file (app.js? server.js? index.js?):
const app = express()
app.use('/static', express.static('public'));
app.use('/files', express.static('public/files'));
Then, in the frontend code:
<img src='/files/myimage.png' />
By the way, there is no problem storing images as base64 in your database... but every database backup will also backup those images. If you have a small database, no worries. However, if you have a LOT of images, especially very large images, this will make your database (and backups) unnecessarily large and unwieldy.
Everything depends on your use case.

Store file in PostgreSQL database using NodeJS and Angular2+

I have read a lot of examples/tutorials on internet that explain how to upload and store files in PostgreSQL using NodeJS, Multer, Sequelize, Express, ...
With none of them I have understood how to do it in my project.
I have an Angular app where I have an input file, I have an API in NodeJS using pg-promise to link with my PostgreSQL database. I have done all my project api with pg-promise so I would like to use it and no Sequelize.
In the database I have read that the best is probably the BYTEA type for files like jpg, png, pdf, docx...
Here is my API hierarchy :
www file contains code to initiate the server
app.js file contains code to initiate express app and the require([library]) of what I need
index.js file contains code to link urls to function that are stored in queries.js
The only thing I want is How to store a file.
Thanks if you can help me
I strongly recommend you the use of multer library. It helps you with the storage of files in disk. You get the path where it is stored, and you could save the path on the database.
https://github.com/expressjs/multer

how to upload a file (excel or csv) this should read the file in nodejs and update in the database mongodb

how to upload a file which should except only excel or csv file and read the file by using nodejs and update in the database table (mongodb)
front end = angular5
backend = nodejs and mongodb
Please help me with this issue.
On the server side i.e node You will need Multer to upload files and set type to uploaded files, fs (Node Inbuilt) library to work with files, You will need csv package to parse data in that file, Mongoose-ODM to save that to MongoDB.
Next time be specific with your answer and show what you have tried till now.

access post files to nodejs without saving to disk

I'm using Heroku to store my Express app and I can't use bodyParser, or formidable, because both uses temp files to store the posted file.
The file I'm posting is simply a large text file (csv). I don't really need to store the file anywhere, because the data will go into mongodb.
Is there anyway to parse the csv file as it is being uploaded?
Starting with Connect 2.9.0 (Express 3.4.0), bodyParser uses multiparty instead of formidable.
In multiparty's default configuration, it does not create temporary files. Read the docs to see exactly how to handle the incoming data, but you'll want to listen to the part event, which gives you a readable stream containing the uploaded file.

Resources