I am facing an issue with my html form that sends a POST request to /upload/upld
in my sails app. The goal is to upload a photo and include the location where the photo was taken as parameters. The upload controller should save the file in a directory named after the location parameter.
<form method="post" action="/upload/upld" enctype="multipart/form-data">
<span class="btn btn-default btn-file wow bounceIn top-buffer">
Browse <input type="file" name="photo">
<input type="hidden" name="location" value="istana" />
</span>
<input type="submit" class="btn btn-primary wow bounceIn top-buffer">
</form>
However, there seems to be an issue with this code. Whenever I try to upload a single file, the upld method appears to run twice based on the console output. This is confusing and I'm unsure why it's happening.
{ location: 'istana', id: undefined }
istana
{ id: undefined }
undefined
Here is what my upload controller currently looks like:
upld: function (req, res) {
var params = req.params.all();
console.log(params);
var this_location = params.location;
console.log(this_location);
req.file('photo').upload({ dirname:require('path').join('./',this_location)},function (err, files) {
if (err){
return res.serverError(err);
}
return res.view('homepage',{
message: files.length + ' file(s) uploaded successfully!',
files: files
}
);
});
}