Server timeout error on uploading videos from Android/IOS to Node Server

谁说胖子不能爱 提交于 2019-12-11 07:04:37

问题


I am trying to upload videos from android/IOS client to NodeJS Server. It works fine for smaller videos but when i try to upload a video lets say, larger than 50 MBs, it throws server timeout error.

One possible solution in my mind is to increase the server timeout limit, but that doesn't seem to be a proper solution. Is there a proper way to upload videos from android without any limitation?

Here is the code that i am using.

exports.create = function(req, res) {
    req.setTimeout(0);

    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, config.uploads.videoUpload.dest);
        },
        filename: function (req, file, cb) {
           let extArray = file.mimetype.split("/");
           let extension = extArray[extArray.length - 1];
           cb(null, Date.now()+ '.' +extension);
        }
    });

    var upload = multer({
        storage: storage,
        limits: config.uploads.videoUpload.limits,
        fileFilter: function (req, file, cb) 
        {
            if (file.mimetype !== 'image/jpeg' && file.mimetype !== 'video/mp4')
            {
               return res.status(400).send({
                  message: 'Only video files are allowed!',
                  error: true
               });
            }
            cb(null, true);
        }
    }).single('video_file');

    if (user)
    {
        // upload function with a callback
        upload(req, res, function(uploadError) {
            if (uploadError)
            {
                return res.status(400).send({
                    message: 'Error occurred while uploading Video',
                    error: true
                });
            } 
            else
            {
                return res.status(200).send({
                    message: 'Video uploaded Successfuly!',
                    error: false
                });
            }
        });
    }
    else 
    {
        res.status(400).send({
            message: 'User is not signed in',
            error: true
        });
    }
};

回答1:


This type of error is often to do with the server or network configuration rather than your code so it is worth checking this config also, and if possible trying with a know working file upload example on the same server also.

For the node muler approach, the following code is tested and definitely work for large video uploads between Android and the server:

// POST: video upload route
// multer approach
var multer = require('multer');
app.use(multer({

    //Set dstination directory
    dest: path.resolve(__dirname, 'public', 'uploaded_videos'),

    //Rename file
    rename: function (fieldname, filename) {
        //Add the current date and time in ISO format, removing the last 'Z' character this usually
        //includes
        var dateNow = new Date();
        return filename + "_" + dateNow.toISOString().slice(0,-1)
    },

    //Log start of file upload
    onFileUploadStart: function (file) {
      console.log(file.originalname + ' is starting ...')
    },

    //Log end of file upload
    onFileUploadComplete: function (file) {
      console.log(file.originalname + ' uploaded to  ' + file.path)
      done=true;
    }

}));

router.post('/web_video_upload', function(req, res) {
    //Log the request details
    //Debug console.log(req.body);
    //Debug console.log(req.files);

    //Send a resposne
    res.send('Video Uploading');
    console.dir(req.files);
});


来源:https://stackoverflow.com/questions/41563664/server-timeout-error-on-uploading-videos-from-android-ios-to-node-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!