I am trying to have my upload button upload a file to a temp directory without needing to click a submit button but i get an error stating : Cannot read properties of undefined (reading 'path').
html.js
<style>
input[type="file"] {
position: absolute;
z-index: -1;
top: 15px;
left: 20px;
font-size: 1px;
color: #b8b8b8;
}
.button-wrap {
position: relative;
}
</style>
<div >
<form id="fileUploadForm" action="/fileUpload"
enctype="multipart/form-data" method="POST">
<label for="upload">Upload File</label>
<input id="upload" name="upload" type="file"
onchange="fileUploadForm.submit()">
</form>
</div>
node.js
app.post('/fileUpload', upload.single('filetoupload'), (req, res, err)=>{
var filepath = req.file.path
filename = './public/temp/' req.file.filename
var jFile = filename '.json'
console.log(req, fileStorage, "filename: ",filename, "filepath: ",filepath, "jFile,",
jFile);
res.status(204).end();
});
CodePudding user response:
Your HTML should be:
<div >
<form id="fileUploadForm" action="/fileUpload" enctype="multipart/form-data" method="POST">
<label for="upload">Upload File</label>
<input id="upload" name="upload" type="file" onchange="fileUploadForm.submit()">
</form>
</div>
CodePudding user response:
Check what's coming on your req object. The error: "Cannot read properties of undefined (reading 'path')." means that the attribute "req.file" is undefined. I beleave that you shold use the name of the input and not the type of the input to access that (in this case "req.filetoupload.path")