An example of the post request method I wrote with node js is as follows. I get the parameters like title, subject, message, email address, name from the request body, but how can I get the file to be sent from the request body as a parameter?
Multer part of my API
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './tosign');
},
filename: function (req, file, cb) {
cb(null , file.originalname);
}
});
const upload = multer({ storage: storage });
Express js post request:
router.post('/sign',(req,res)=>{
res.send('Sign request is called');
var title = req.title;
var subject = req.subject;
var message = req.message;
var email_address = req.email_address;
var name = req.name;
var file = req.files.file;
const opts = {
test_mode: 1,
title: title,
subject: subject,
message: message,
signers: [
{
email_address: email_address,
name: name
}
],
//TODO - files from post request
files: ['file.pdf']
};
hellosign.signatureRequest.send(opts).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err)
});
});
You can use multer which is a nodejs middleware to handle file uploading, there are many options available but I think multer is the most reliable option. You can find about it in the below mentioned link: https://www.npmjs.com/package/multer
After integrating it, you will be able to get files in req.files
Try below mentioned code:
const multer = require('multer');
const upload = multer({ dest: "src/uploads" });
// In case of file array
const uploadConfig = upload.array("profilePic", 12);
// In case of single file you can use
const uploadConfig = upload.single("profilePic");
router.use(uploadConfig);
router.post('/sign',(req,res)=>{
// req.file if you have used upload.single
// req.files if you have used array
console.log('req', req.file);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.