//<html>
//<head>
//<title> File Uploader</title>
//</head>
//<body>
//<form action="http://localhost:3000/upload" enctype="multipart/form-data" method="POST">
//<input type="file" name="data" />
//<input type="submit" value="Upload a file" />
//</form>
//</body>
//</html>
//Middleware
var multer = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, "/media/uploads/");
},
filename: function (req, file, callback) {
var actualFileName = file.originalname;
var actualFileNameSplit = actualFileName.split(".");
var actualFileName0 = actualFileNameSplit[0];
var id =
Date.now() +
"-" +
actualFileName0 +
"-" +
path.extname(file.originalname);
var finalId = id.replace(/s/g, "");
callback(null, finalId);
},
});
// var maxSize = 200 * 1024 * 1024;
var upload = multer({
storage: storage,
// limits: { fileSize: maxSize },
}).single("file");
// req.file is the `file` file
// req.body will hold the text fields, if there were any
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
return res.status(500).send({
message: "Error",
statusCode: "500",
});
} else if (err) {
// An unknown error occurred when uploading.
return res.status(500).send({
message: "Error",
statusCode: "500",
});
} else {
return res.status(200).send({
message: "File Uploaded!",
statusCode: "200",
});
}