I am trying to upload a file to a server via Multer. All this code does is it creates the folder but not the file. I also tried to use a tutorial on github but no luck either My code on frontend
<p class="font-weight-bolder mb-3">Upload Screenshot
<form enctype="multipart/form-data"></form><input id="uploadFile" type="file" name="image" />
</p>
The code AJAX
$('#uploadFile').on('change', function(){
var file = $(this).get(0).files[0];
console.log(typeof file);
console.log(file)
console.log(file.name)
var formData = new FormData();
formData.append('Upload', file, file.name);
$.ajax({
url: $(this).attr('action') || window.location.pathname + '/repr_screenshot',
type: "POST",
data: file,
cache: false,
processData: false,
success: function(data){
console.log("success")
},
error: function(data){
console.log("file not uploaded")
}
});
})
And the MAIN.JS
var multer = require('multer')
var storage = multer.diskStorage({
destination: 'views/repr/mixin/screenshots',
filename: '/file.png'})
var upload = multer({storage: storage,
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
}
});
router.post('/repr_screenshot', upload.single('image'), function (req, res, next) {
console.log(req.file);
return false;
})
The console.log output I get from frontend:
Object
File {name: "Capture.PNG", lastModified: 1597907180050, lastModifiedDate: Thu Aug 20 2020 10:06:20 GMT+0300 (Eastern European Summer Time), webkitRelativePath: "", size: 89172, …}
Capture.PNG
And the output of console.log(req.file) is "Undefined"