使用angularjs和nodejs的正文解析器进行多方文件上传问题

时间:2019-05-16 10:21:44

标签: node.js angularjs body-parser multiparty

尝试将文件从Angularjs UI上传到nodejs服务器,但遇到bodyparser的问题,从而增加了抛出的限制-

  

“ SyntaxError:意外令牌-JSON中的位置0”,

如果未添加限制,则会引发-

  

“有效载荷太大”

我正在使用连接多方中间件上载文件。在bodyparser中尝试了{limit:'50mb'},也没有任何限制。

UI代码-

$('#imgupload').on('change', function  (evt) {
   let uploadedFiles = evt.target.files;
   let formData = new FormData();
    for (var i = 0; i < uploadedFiles.length; i++) {
        formData.append("uploads[]", uploadedFiles[i], 
        uploadedFiles[i].name);
    }
   let url =   "/upload";
   httpService.restApi(url,formData)
       .then(function (response) {
           console.log("the file has been uploaded to local server 
           ",response);
   });
});

Nodejs(服务器代码)-

const  multipart  =  require('connect-multiparty');  
const  multipartMiddleware  =  multipart({ uploadDir:  './uploads' });

app.use(bodyParser.json({limit: '50mb'}));

app.use(bodyParser.urlencoded({extended: true}));

app.post('/upload', multipartMiddleware, (req, res) => {  
    res.json({
        'message': 'File uploaded succesfully.'
    });
});

2 个答案:

答案 0 :(得分:0)

从路径中删除bodyParser中间件:

const  multipart  =  require('connect-multiparty');  
const  multipartMiddleware  =  multipart({ uploadDir:  './uploads' });

̶a̶p̶p̶.̶u̶s̶e̶(̶b̶o̶d̶y̶P̶a̶r̶s̶e̶r̶.̶j̶s̶o̶n̶(̶{̶l̶i̶m̶i̶t̶:̶ ̶'̶5̶0̶m̶b̶'̶}̶)̶)̶;̶

̶a̶p̶p̶.̶u̶s̶e̶(̶b̶o̶d̶y̶P̶a̶r̶s̶e̶r̶.̶u̶r̶l̶e̶n̶c̶o̶d̶e̶d̶(̶{̶e̶x̶t̶e̶n̶d̶e̶d̶:̶ ̶t̶r̶u̶e̶}̶)̶)̶;̶

app.post('/upload', multipartMiddleware, (req, res) => {  
    res.json({
        'message': 'File uploaded succesfully.'
    });
});

内容为application/form-data,而不是application/jsonapplication/x-www-form-urlencoded

答案 1 :(得分:0)

如果您使用multer并使用$ http进行API调用,将“ Content-Type”标头明确设置为multipart / form-data,则会收到“ Multer:找不到边界错误”,并且删除了“ Content-Type” ”标头或将其设置为false会引发-“将圆形结构转换为JSON错误”。因此,我使用“提取”来进行API调用,因为它会自动识别“ Content-Type”。

UI代码(如下修改)-

$('#imgupload').unbind('change').on('change', function  (evt) {
    evt.stopPropagation();
    evt.preventDefault();
   let uploadedFiles = evt.target.files;


   let formData = new FormData();
   for(let i=0; i<uploadedFiles.length;i++){
    formData.append("uploads", uploadedFiles[i], uploadedFiles[i].name);
   }

   let url = '/upload';
   var req = {
    method: 'POST',
    body: formData
   }

   fetch(url,req).then(function(response) {
        console.log("the file has been uploaded to local server ",response);
        $scope.uploadToSftp();
   });

});

Nodejs代码(如下修改)-

const multer = require('multer');
const storage = multer.diskStorage({
 destination: function (req, file, cb) {
   cb(null, './uploads/')
 },
 filename: function (req, file, cb) {
   cb(null, file.originalname)
 }
})
const multipartMiddleware  =  multer({ storage:  storage });

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({  
  extended: true
}));

app.post('/upload', multipartMiddleware.array("uploads",2), function(req, res) 
{
  console.log("the upload api is called");
  return res.send(req.files);
});