将视频从 Android/IOS 上传到节点服务器时出现服务器超时错误 [英] Server timeout error on uploading videos from Android/IOS to Node Server

查看:72
本文介绍了将视频从 Android/IOS 上传到节点服务器时出现服务器超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视频从 android/IOS 客户端上传到 NodeJS 服务器.它适用于较小的视频,但是当我尝试上传大于 50 MB 的视频时,它会引发服务器超时错误.

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.

我认为一种可能的解决方案是增加服务器超时限制,但这似乎不是一个合适的解决方案.是否有没有任何限制地从 android 上传视频的正确方法?

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
        });
    }
};

推荐答案

这种类型的错误通常与服务器或网络配置有关,而不是与您的代码有关,因此也值得检查此配置,如果可能,请尝试使用也知道在同一台服务器上的工作文件上传示例.

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.

对于节点 muler 方法,以下代码经过测试,绝对适用于 Android 和服务器之间的大型视频上传:

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);
});

这篇关于将视频从 Android/IOS 上传到节点服务器时出现服务器超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆