简体   繁体   中英

PhoneGap upload photos: Cannot read property 'file' of undefined

I'm trying to create an application that will upload a photo to a mongodb server, here is my code:

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

and on the node server side this is where the error seems to happen:

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

};

so I'm having the error Cannot read property 'file' of undefined

I think it is an error related to the version of the express that you are using. In previous version of express (3.x) you should include :

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

so you can access to req.files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM