我正在创建一个具有简单文件上传系统的网站。我想在上传之前从请求的图像中检查exif数据,因为如果没有exif数据,则向用户发送错误消息并停止上传过程。我正在使用node-exif检查exif数据,并使用express-fileupload将文件上传到目录。
HTML:
<form method="post" ref="uploadForm" enctype="multipart/form-data">
<div class="select-btn-wrapper">
<input type="file" name="selectedFile" id="select_file" accept="image/*" capture="camera" />
<button class="select_btn">Choose</button>
</div>
<input type="submit" name="submit_file" class="submit_file" value="Upload">
</form>
server.js
app.post('/', (req, res) => {
if(!req.files)
return res.status(400).send('No files were uploaded.')
var dateTimeNow = Date.now()
var dateString = dateTimeNow.toString()
var selectedFile = req.files.selectedFile
var filename = dateString + "_" + selectedFile.name
var filepath = path.join(uploadsPath, filename)
selectedFile.mv(filepath, function (err) {
if (err)
res.status(500).send(err)
try {
new ExifImage({ image: filepath }, (err, exifData) {
if (err)
console.log("EXIF IMAGE ERROR: " + err)
console.log(exifData)
})
}
res.send('File uploaded')
})
})