multer无法读取属性'路径'从iPhone库上传图片时未定义的

时间:2016-04-07 02:19:45

标签: ios node.js express multer

我使用multer在我的项目中进行ajax上传,并在前端使用FormData进行ajax上传。

我在iPhone 5s中进行测试

我的问题是当我点击input[type="file"]时,我选择拍照,可以成功上传。

但是当我从我的画廊中选择并选择之前拍摄的照片时,上传程序崩溃了

这个问题只会在iPhone中引起,但不会在Android或PC中引起

\routes\index.js:128

filename: req.file.path.replace(/public/, '')
                   ^

TypeError: Cannot read property 'path' of undefined
    at E:\GraduationProject\jnstagram\routes\index.js:128:23
    at Array.<anonymous> (E:\GraduationProject\jnstagram\node_modules\multer\lib\make-middleware.js:52:37)
    at listener (E:\GraduationProject\jnstagram\node_modules\on-finished\index.js:169:15)
    at onFinish (E:\GraduationProject\jnstagram\node_modules\on-finished\index.js:100:5)
    at callback (E:\GraduationProject\jnstagram\node_modules\ee-first\index.js:55:10)
    at IncomingMessage.onevent (E:\GraduationProject\jnstagram\node_modules\ee-first\index.js:93:5)
    at emitNone (events.js:80:13)
    at IncomingMessage.emit (events.js:179:7)
    at endReadableNT (_stream_readable.js:913:12)
    at _combinedTickCallback (node.js:383:13)
    at process._tickCallback (node.js:407:11)

我的服务器端代码:

var express = require('express');
var multer = require('multer');

var router = express.Router();

module.exports = function() {

    var storage = multer.diskStorage({
        destination: function(req, file, cb) {
            cb(null, 'public/uploads');
        },
        filename: function(req, file, cb) {
            var name = file.originalname;
            var dot = name.lastIndexOf('.');
            cb(null, name.slice(0, dot) + '-' + new Date().getTime() + name.slice(dot));
        }
    });

    var upload = multer({
        storage: storage,
        fileFilter: function(req, file, cb) {
            if (/\.(png|jpg)$/i.test(file.originalname)) {
                cb(null, true);
            } else {
                cb(null, false);
            }
        }
    });

    var fileUpload = upload.single('file');

    router.post('/ajaxupload', function(req, res) {
        fileUpload(req, res, function(err) {
            if (err) {
                res.json({
                    success: false,
                    message: 'upload failed'
                });
                return;
            }
            res.json({
                success: true,
                filename: req.file.path.replace(/public/, '')
            });
        });
    });

    return router;

};

我的客户端上传代码:

var fd = new ForData();
fd.append('file', this.refs.myFile.files[0]); // use React refs
request
    .post('/ajaxupload')
    .send(fd)
    .end(function(err, res) {
        if (err) {
            throw err;
        }
           // do something
    });

0 个答案:

没有答案