我有一个html格式的文件上传表,提交后,我将使用户输入的文件作为formData,我将触发从angularjs的nodejs api。
在nodejs中-我想接收formData并上传到一些外部存储中。该外部存储还要求输入文件为formData。
但是我无法在nodejs中接收到作为formData的formData。
请任何人提出解决方案。
$scope.add = function(file) {
var fd = new FormData();
fd.append('file', file);
$http.post('/uploadFile', fd, {headers: {'Content-Type': undefined}}).then(function(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
}
NodeJS
app.post('/uploadFile', function (req,res) {
console.log(req.body); //Undefined
})
HTML
<form name="addForm" ng-submit="add(videoFile)">
<input class="file-upload__input" type="file" file-model="videoFile" required>
</form>
发布于 2018-02-07 18:30:29
向fd
变量添加作用域,如下所示,
将您当前的fd
声明从
var fd = new FormData();
致,
$scope.fd = new FormData();
在post时,将其作为范围变量进行post,如下所示
$http.post('/uploadFile', $scope.fd, {headers: {'Content-Type': undefined}}).then(function(response) {
希望这能有所帮助!
发布于 2018-02-07 20:15:07
如图所示的in this repository
要记录req.files
服务器端:
const formData = require('express-form-data');
const multipartyOptions = {
autoFiles: true
};
// parse a data with connect-multiparty.
app.use(formData.parse(multipartyOptions));
// clear all empty files (size == 0)
app.use(formData.format());
// change file objects to node stream.Readable
app.use(formData.stream());
// union body and files
app.use(formData.union())
客户端:
<input id="i1" type="file">
和
formData = new FormData();
formData.append('image', i1.files[0]);
$http.post('http://localhost:3000/upload', formData, { ... omissis ... })
https://stackoverflow.com/questions/48661197
复制相似问题