explore-services/services/upload/uploadService.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

var express = require("express");
var bodyParser = require("body-parser");
var multer = require("multer");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post("/upload", multer({dest: "./uploads/"}).array("uploads[]", 12), function(req, res) {
var filepath = __dirname+"/"+req.files[0].path;
console.info(req.files[0]);
console.info("PAth:::::::"+filepath);
// var responseData;
// fs = require('fs')
// fs.readFile(__dirname+"/"+req.files[0].path, 'utf8', function (err,data) {
// if (err) {
// responseData= "error";
// console.log(err);
// }
// console.log(data);
// responseData=data;
// });
// req.files[0].data=responseData;
// res.send(req.files);
res.sendFile(filepath);
var fs = require('fs');
fs.stat(filepath, function (err, stats) {
console.log(stats);//here we got all information of file in stats variable
if (err) {
return console.error(err);
}
fs.unlink(filepath,function(err){
if(err) return console.log(err);
console.log('file deleted successfully');
});
});
});
app.get('/get/d6bd36a06762e2f0da891f9670fcb2b0', function(req, res) {
res.sendFile('uploads/d6bd36a06762e2f0da891f9670fcb2b0')
})
var server = app.listen(8000, function() {
console.log("Listening on port %s...", server.address().port);
});