I am new to this thing, can anyone explain me that, if i am hitting an API endpoint from angular to send a image/video. How should i write my route.post
route in express.js to get the incoming image/video.
I am using the FileTransfer Ionic native plugin to send the captured image/video to my server.
this.mediaCapture.captureImage()
.then((data: MediaFile[]) => {
let option = {
enableHighAccuracy : true
}
this.geolocation.getCurrentPosition(option)
.then((resp) => {
let ionic: LatLng = new LatLng(resp.coords.latitude, resp.coords.longitude);
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'File',
fileName: data['0'].name,
params: {
postTitle: 'hey there',
postDesc: 'jshfdjnsbdkjcfweigfc,ndsbfckjg',
postLat : ionic.lat,
postLng : ionic.lng,
userId : this.loggedInUserId
}
}
fileTransfer.upload(data['0'].fullPath, 'https://***************.com/images/postImages', options)
.then((data) => {
console.log('image has been uploaded');
}, (err) => {
// error
})
})
})
first i am capturing the image then the location coordinates and then i am sending the file and data to the API enpoint.
router.post('/postImages', function(req, res) {
pool.getConnection(function(err, connection) {
console.log(req.body);
var sql = "INSERT INTO posts (post_title, post_desc, post_lat, post_lng, user_id) VALUES (?, ?, ?, ?, ?)";
var inserts = [req.body.postTitle, req.body.postDesc, req.body.postLat, req.body.postLng, req.body.userId];
sql = mysql.format(sql, inserts);
console.log(sql);
connection.query(sql, function(error, results) {
res.json(results);
connection.release();
if(error) throw error;
});
});
});
the API hit is properly done but i am not reciving any thing in the req body, also there is no file in the directory. Please help me i am at the brink of my Final Year Project submission.