I am working on mean stack application and I face one issue with file uploading.
I want to stop all uploading immediately as soon as the global.isProcessKilled variable is true.
My code is as bellow
photo.js
var express = require("express");
var app = express()
var router = express.Router();
var multer = require("multer");
var mongoose = require("mongoose");
var fs = require("fs");
var jwt = require("jsonwebtoken");
var lib = require("../../../config/lib");
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, dirPath);
},
filename: function(req, file, cb) {
var datetimestamp = Date.now() + Math.floor(Math.random());
cb(null, datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1].toLowerCase());
}
});
var photoUpload = multer({
storage: storage,
fileFilter : function (req, file, cb) {
if(global.isProcessKilled){ //my custom validation flag
// To reject this file pass `false`, like so:
cb(null, false);
}else{
// To accept the file pass `true`, like so:
cb(null, true);
}
}
});
router
.route("/api/photo/photoUpload")
.post(
photoUpload.array("file"),
function(req, res) {
.....
.....
....
});
By this code file uploading is working fine but my next original API call occurs only after processing the current(previous) file.
More information: All selected files are more than 1mb and less than 10mb
- Step 1 : upload 30 file simultaneously
- Step 2 : the process will start
- Step 3 : suppose
global.isProcessKilledvariable will betrueat 10 files - Step 4 : next all files will not process for upload because of filter
- Step 5 : but the previous file means 9th file is running and I don't know how to stop it
- Step 6 : the process will not call next callback until file no 9th will not be uploaded and it takes about 2min
- Step 7 : for this reason api response I too delay
Please help me to resolve this issue.