[英]PhoneGap upload photos: Cannot read property 'file' of undefined
我正在尝试创建一个将照片上传到mongodb服务器的应用程序,这是我的代码:
upload = function (imageURI) {
var ft = new FileTransfer(),
options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = 'filename.jpg'; Node at the server side.
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.params = {
"description": "Uploaded from my phone"
};
// alert(imageURI);
// alert(serverURL);
ft.upload(imageURI, serverURL + "/images",
function (e) {
getFeed();
},
function (e) {
alert("Upload failed");
}, options);
}
在节点服务器端,这似乎是发生错误的地方:
exports.addImage = function(req, res, next) {
var file = req.files.file,
filePath = file.path,
lastIndex = filePath.lastIndexOf("/"),
tmpFileName = filePath.substr(lastIndex + 1),
image = req.body,
images = db.collection('images');
image.fileName = tmpFileName;
console.log(tmpFileName);
images.insert(image, function (err, result) {
if (err) {
console.log(err);
return next(err);
}
res.json(image);
});
};
所以我遇到了无法读取未定义的属性“文件”的错误
我认为这是与您使用的Express版本有关的错误。 在Express(3.x)的早期版本中,您应该包括:
app.use(express.methodOverride()); app.use(express.multipart());
这样您就可以访问req.files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.