How to upload a downloaded file to a Node.js Express application using angular -file-upload

I want to upload a file using angular-file-upload. But when I try to access req.body.filesor req.files, I get undefined.

Can someone tell me how to get a file that is uploaded via angular downloads in the node.js express app?

The payload request is as follows:

Content-Disposition: form-data; name="myFile"; filename="BroadcomLogo.gif"
Content-Type: image/gif

JS Code:

$scope.upload[index] = $upload.upload({
   url : '/upload',
   method: 'POST',
   file: $scope.selectedFiles[index],
   fileFormDataName: 'myFile'
});

Node Code

upload: function(req, res) {
    console.log(req.files);
    res.send("Hello");
},
+4
source share
2 answers

You need to use middleware that parses the files from the request in Node.js:

var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.post('/upload', multipartMiddleware,upload);

, middleware - , . connect.createServer, . , ServerResponse . / , next().

http://stephensugden.com/middleware_guide/

next(), .

, : npm install connect-multiparty

, :

, , ,

+8

files name :

req.files.myFile
-4

Source: https://habr.com/ru/post/1547409/


All Articles