1
0

fileUpload.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //© 2019 Dublin City University, Trinity College Dublin. All rights reserved. This material may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder.
  2. import multer from 'multer';
  3. import path from 'path';
  4. const destination = __dirname+'/upload/';
  5. //const destination = path.normalize(path.resolve(__dirname))+'/upload/';
  6. let storage = multer.diskStorage({
  7. destination: function (req, file, callback) {
  8. callback(null, destination);
  9. },
  10. filename: function (req, file, callback) {
  11. let timeStamp = Date.now();
  12. let fileExtenstion =path.extname(file.originalname);
  13. let fileBaseName = path.basename(file.originalname, path.extname(file.originalname));
  14. callback(null, fileBaseName+'-'+timeStamp+fileExtenstion);
  15. }
  16. });
  17. let upload = multer({ storage : storage}).any();
  18. class fileUploadsController {
  19. upload(req, res) {
  20. upload(req,res,function(err) {
  21. if(err) {
  22. return res.status(404).send({
  23. message: 'File Upload Failed.'
  24. });
  25. }
  26. else
  27. {
  28. return res.status(200).send({
  29. message: 'File Upload Successful.',
  30. fileName: req.files[0].filename,
  31. filePath : destination
  32. });
  33. }
  34. });
  35. }
  36. }
  37. const fileUploadController = new fileUploadsController();
  38. export default fileUploadController;