Sails.js:如何知道请求是否包含要上传的文件? [英] Sails.js: How to know if the request contains a file to upload?
本文介绍了Sails.js:如何知道请求是否包含要上传的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
req.file()
中引发超时错误的文件。 upload:function(req,res){
res.setTimeout(0);
function onCreateCover(err,cover){
if(err){
return res.negotiate(err);
} else {
res.status(201);
return res.json(cover);
}
}
if(/^multipart\/form-data/.test(req.headers['content-type'])){
req.file('image')。upload({
maxBytes:2000000,
dirname:coverDir
},function(err,uploadedFile){
if(err){
res.serverError(err);
} else if(uploadedFile.length> 0){
Cover.create({
title:req.body.title,
描述:req.body.description,
路径:uploadedFile [0] .fd
})。exec(onCreateCover);
}
});
} else {
Cover.create(req.body).exec(onCreateCover);
}
}
我想知道是否存在一种方法或方法检查请求是否真的包含一个文件
解决方案
我不知道是否有更好的方法,但我使用这个: / p>
if(req._fileparser.upstreams.length){/ *上传代码* /}
I have the next action in one of my controllers, its function is upload a image file or create a new cover model using a video url, its work fine but when the request has a header Content-Type: multipart/form-data and does not contains a file the action throw a timeout error in req.file()
.
upload: function(req, res) {
res.setTimeout(0);
function onCreateCover(err, cover) {
if (err) {
return res.negotiate(err);
} else {
res.status(201);
return res.json(cover);
}
}
if (/^multipart\/form-data/.test(req.headers['content-type'])) {
req.file('image').upload({
maxBytes: 2000000,
dirname: coverDir
}, function(err, uploadedFile) {
if (err) {
res.serverError(err);
} else if (uploadedFile.length > 0) {
Cover.create({
title: req.body.title,
description: req.body.description,
path: uploadedFile[0].fd
}).exec(onCreateCover);
}
});
} else {
Cover.create(req.body).exec(onCreateCover);
}
}
I want to know if exist a method or way to check if request really contains a file
解决方案
I don't know if there is a better way, but I use this:
if (req._fileparser.upstreams.length) { /* upload code */ }
这篇关于Sails.js:如何知道请求是否包含要上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文