I want to upload a file using angular-file-upload. But when I try to access req.body.files
or req.files
i get undefined
.
Can anybody tell me how to get the file which is uploaded through angular-file-upload in node.js express app ?
Request Payload is as below:
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");
},
You need to use a middleware that parses files from a request in Node.js:
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.post('/upload', multipartMiddleware,upload);
Simply put, middleware are functions that handle requests . A server created by connect.createServer can have a stack of middleware associated with it. When a request comes in, it is passed off to the first middleware function, along with a wrapped ServerResponse object and a next callback. Each middleware can decide to respond by calling methods on the response object, and/or pass the request off to the next layer in the stack by calling next() .
In the case of the multipart middleware, it will call the next() method so you can write your own function to respond to the request.
Of course you need to install it first: npm install connect-multiparty
They are other middlewares that handle multipart uploads:
You should probably use one that is compatible with connect, as express is built on top of connect
使用name
属性作为键在files
对象中访问它:
req.files.myFile
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.