簡體   English   中英

PhoneGap上傳照片:無法讀取未定義的屬性“文件”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM