Multer - 节点JS - 上传图像和数据

时间:2017-11-17 13:29:03

标签: javascript angularjs node.js multer

我尝试使用Node JS,Multer和Angular JS将图像上传到我的服务器。我面临的问题是无论我做什么,“req.body”在服务器端总是空的。我需要发送图像以及一些信息(例如密码),但是当它到达服务器时,所有内容都是未定义的。尽管如此,图像仍然正确上传。

有人可以建议如何在同一个电话上同时发送一些数据和图像吗? 提前谢谢!

编辑:这不是重复的问题

 $scope.uploadImage = function() {

      var fd = new FormData();

      var imgBlob = dataURItoBlob($scope.uploadme);
      fd.append('api_password', 'bombita');
      fd.append('file', imgBlob, $scope.theFile.name);


      $http.post(
          'http://45.33.116.147:4000/upload_aviso',
          fd, {

            transformRequest: angular.identity,
            headers: {
              'Content-Type': undefined
            }


          }
        )
        .success(function(response) {
          $scope.result_photo_upload = "Photo Successfully uploaded"
        })
        .error(function(response) {
          $scope.result_photo_upload = "Error uploading picture : " + JSON.stringify(response)
        });
    }

编辑2:添加服务器端代码。

 const PORT=4000;
    var express = require('express'); 
    var app = express(); 
    var bodyParser = require('body-parser');
    var multer = require('multer');
    var PASSWORD;
    const fileSys = require('fs');
    fileSys.readFile('/etc/api_password.txt', 'utf8', function(err, data) {  
     if (err) throw err;
        PASSWORD = data.trim();

    });

    app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });
    /** Serving from the same express Server
    No cors required */
    app.use(express.static('../client'));
    app.use(bodyParser.json());
    var storage = multer.diskStorage({ //multers disk storage settings
        destination: function (req, file, cb) {
            cb(null, '/var/www/images/')
        },
        filename: function (req, file, cb) {
            var datetimestamp = Date.now();
        console.log("File name : " + JSON.stringify(file));
            cb(null, "foto_avisos_" +  file.originalname)
        }
    });
    var upload = multer({ //multer settings
                    storage: storage
                }).single('file');
    /** API path that will upload the files */
    app.post('/upload_aviso', function(req, res) {
        console.log("API_password : " + JSON.stringify(req.body));
        upload(req,res,function(err){
            if(err){
            res.header('Access-Control-Allow-Origin', '*');
            res.header('Access-Control-Allow-Methods', '*');
            res.header('Access-Control-Allow-Headers', 'Content-Type'); 
                res.header('Content-Type', 'text/html'); 
                res.status(500).json({error_code:1,err_desc:err});
            return;
            }
             res.json({error_code:0,err_desc:null});
        });

    });

    app.options('*', function(req, res) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', '*');
        res.header('Access-Control-Allow-Headers', 'Content-Type');
        res.status(200).send("OK");
    }); 
    app.listen(PORT, function(){
        console.log('Application running on ' + PORT + '...');
    });

0 个答案:

没有答案