How we upload File with some data to nodeJS using Simple Form

Configuration

first you have to install some libraray to your to your project

npm i express multer mongoose

index.js

app.use(express.static(__dirname(‘/public’))
//set folder as static folder
app.use(express.urlencoded({extend…


This content originally appeared on DEV Community and was authored by Deepak

Configuration

first you have to install some libraray to your to your project

npm i express multer mongoose

index.js

app.use(express.static(__dirname('/public'))
//set folder as static folder
app.use(express.urlencoded({extended:false}))
//set req.body of data are accessible from index.html

<form action="/api/post" method="post" enctype="multipart/form-data" >
<input type="text" name="name"/>
<input type="email" name="email"/>
<input type="file" name="file" id="file" />
<nput type="submit" />
</form>

index.js

`const upload = multer({ dest: 'uploads/' })

const app = express()
//this code for single file upload
app.post('/api/post', upload.single('file'), function (req, res, next) {
const {name,email}=req.body
// req.file is the file file
res.send({name,email,filename:req.file.filename})
// req.body will hold the text fields, if there were any
})

`


This content originally appeared on DEV Community and was authored by Deepak


Print Share Comment Cite Upload Translate Updates
APA

Deepak | Sciencx (2021-12-18T07:50:27+00:00) How we upload File with some data to nodeJS using Simple Form. Retrieved from https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/

MLA
" » How we upload File with some data to nodeJS using Simple Form." Deepak | Sciencx - Saturday December 18, 2021, https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/
HARVARD
Deepak | Sciencx Saturday December 18, 2021 » How we upload File with some data to nodeJS using Simple Form., viewed ,<https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/>
VANCOUVER
Deepak | Sciencx - » How we upload File with some data to nodeJS using Simple Form. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/
CHICAGO
" » How we upload File with some data to nodeJS using Simple Form." Deepak | Sciencx - Accessed . https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/
IEEE
" » How we upload File with some data to nodeJS using Simple Form." Deepak | Sciencx [Online]. Available: https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/. [Accessed: ]
rf:citation
» How we upload File with some data to nodeJS using Simple Form | Deepak | Sciencx | https://www.scien.cx/2021/12/18/how-we-upload-file-with-some-data-to-nodejs-using-simple-form/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.