3

I implemented the multer upload example on restful using express. The following code is the server code.

var storage = multer.diskStorage({
        destination: function (req, file, cb) {
          cb(null, './files/')
        },
        filename: function (req, file, cb) {
          crypto.pseudoRandomBytes(16, function (err, raw) {
            cb(null, raw.toString('hex') + Date.now() + '.xsd');
          });
        }
});

var cpUpload = multer({ storage: storage, limits: {fileSize: 1000000}}).single('schema-file');

var app = express(); 
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");
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
        next();
});

app.post('/ws-api-designer-upload-files', cpUpload, function (req, res, next) {

        console.log("filepath " + req.file.filename);
        cpUpload(req,res,function(err){

            if(err){
                res.set('Content-Type', 'application/json');
                res.end(JSON.stringify({
                    status: 'ERROR 1',
                    description: err.message
                }));
                return;
            }else{
                res.set('Content-Type', 'application/json');
                res.end(JSON.stringify({
                    status: 'SUCCESS 1',
                    description: 'success'
                }));
                return;             
            }
        });

});

But when i call this service using postman, the file are uploaded but when i compare original file between uploaded file, the uploaded file corrupted. I'm struggling on this problem for almost 1 week, please help.

Note that this problem comes when code running on windows server 2008 r2. So when i run this server code on localhost there was no any problem like corrupted file. And I only upload a xsd file which is 173kb in size.

This one is uploaded file (corrupt) uploaded file

This one is original file original file

dityagow
  • 69
  • 12

0 Answers0