gpt4 book ai didi

android - 将视频从 Android/IOS 上传到 Node 服务器时出现服务器超时错误

转载 作者:行者123 更新时间:2023-11-29 11:49:59 24 4
gpt4 key购买 nike

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

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

这是我正在使用的代码。

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

最佳答案

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

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

// 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 - 将视频从 Android/IOS 上传到 Node 服务器时出现服务器超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41563664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com