Mahdi.Pishguy Mahdi.Pishguy - 3 months ago 11
Javascript Question

NodeJs Simple upload file dont return correct result

The code below is my Node.js upload file. This code works fine and can upload any file from client, but after upload, I can't get upload status result. For example on Node.js, after upload file, I can't send uploading result to client.

var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3000;
var mysql = require('mysql');
var multer = require('multer');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'a',
database: 'signal'
});

var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, req.params.storeId);
}
});
var upload = multer({storage: storage}).single('userPhoto');

app.post('/newVitrine/:storeId/:userId', function (req, res) {
upload(req, res, function (err) {
if (err) {
res.send({'result': 0});
console.log({'result': 0});
} else {
res.send({'result': 1});
console.log({'result': 1});
}
});
});

server.listen(port, function () {
console.log('Server listening at port %d', port);
});


Client should get json object as
{'result': 1}
but instead I get long json object and
result
isn't in to that.

Answer

Short answer: Tere is nothing you can do. You can use ajax and fake it; make it look like it is working as expected.Even file uploading services does that - such as transloadit.

The problem is a very general problem actually, if multer hits a size limit exceed, that happens(though my situation was a little bit different), it aborts the connection and that causes the problem; you can not send response to the client on aborted connection.

Check this perfect answer which explains a lot :

How to cancel HTTP upload from data events?

Here is the working example:

var express = require('express');
var app = express();
var server = require('http').createServer(app);

var multer = require('multer');


var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './temp')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
var upload = multer({storage: storage}).single('upload');

app.get('/', function (req, res) {

    res.send(`<form action="/upload" enctype="multipart/form-data" id="upld" method="post">
        <input type="text" name="title"><br>
        <input type="file" name="upload" multiple="multiple"><br>
        <input type="submit" value="Upload" id="upldbtn">
        </form>        
        `
    );
});


app.post('/upload', function (req, res) {
    upload(req, res, function (err) {
        if (err) {
            res.send({'result': 0});
            console.log(err);
        } else {
            res.send({'result': "custom result"});
            console.log({'result': 1});
        }
    });
});

server.listen("9090", function () {
    console.log('Server listening at port');
});
Comments