PhoneGap上传照片:无法阅读属性'文件'未定义的

时间:2014-09-30 21:57:05

标签: javascript mongodb cordova

我正在尝试创建一个将照片上传到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);
    });

};

所以我有错误无法读取属性' file'未定义的

1 个答案:

答案 0 :(得分:1)

我认为这是与您正在使用的快递版本相关的错误。在以前版本的express(3.x)中,您应该包括:

app.use(express.methodOverride()); app.use(express.multipart());

所以你可以访问req.files

相关问题