节点.js 使用 Multer 上传图像显示未定义
node.js to upload image using multer shows undefined
在这里,我正在尝试从 2 个不同的文件输入上传文件,我可以将其上传到前端,但在后端它仍然undefined
.尝试了几件事,但没有奏效。
.html:
<input type="file" name="file1" file-model = "file1"/>
<input type="file" name="file2" file-model = "file2"/>
<button ng-click = "uploadFile()">UPLOAD FILES</button>
命令:
angular.module('myApp').directive('fileModel', ['$parse',
function($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function() {
scope.$apply(function() {
modelSetter(scope, element[0].files[0]);
});
});
}
};
}
])
服务:
angular.module('myApp').service('fileUpload', ['$http',
function($http) {
this.uploadFileToUrl = function(file, uploadUrl) {
var fd = new FormData();
fd.append('file1', file);
fd.append('file2', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined
}
})
}
}
]);
控制器:
angular.module('myApp')
.controller('ContactCtrl', ['$scope', 'fileUpload', '$http',
function($scope, fileUpload, $http) {
$scope.uploadFile = function() {
var file1 = $scope.file1;
var file2 = $scope.file2;
console.log('file is ');
console.dir(file1);
console.dir(file2);
var uploadUrl = '/upload';
fileUpload.uploadFileToUrl({
file1, file2
}, uploadUrl);
};
服务器:
function upload(req,res){
console.log(req.files.file1);
}
在
nodejs的服务器端找不到文件的一种情况,即您可以在 /api/v1/uploadfile": [{
method: "POST",
action: controllers.advertController.videoUpload,
middleware: [multipartMiddleware],
views: {
json: views.jsonView
}
}],
global.multipartMiddleware = multipart();
在服务器端代码在请求的中间件中使用多部分中间件。
控制器(在此将正确的参数传递给服务)
var file ={}
file.file1=file1;
file.file2=file2;
fileUpload.uploadFileToUrl(file, uploadUrl);
服务(如果要一次上传多个文件使用循环)
angular.module('myApp').service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file1', file.file1);
fd.append('file2', file.file2);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
}
}]);
相关文章:
- 非常大的图像显示渐进
- CSS Html使图像显示在单选按钮的选项上(单选按钮被CSS隐藏)
- JS照片库.使大图像显示和缩略图可单击
- 多个图像显示带有动态创建的画布的最后一个图像并分配给图像
- 当我在jquery/javascript中单击按钮时加载图像显示
- React图像显示
- 随机图像显示,无重复,使用Javascript
- 将跨度内容(图像url)转换为图像(显示)
- 随机图像显示不止一次
- 节点.js 使用 Multer 上传图像显示未定义
- 范围滑块,用于更改轨道上方对齐圆的直径并使对齐的图像显示在下方
- 使用图像显示和隐藏
- 使用 JavaScript 将文件夹中的图库图像显示到网页中
- 画布到图像显示为无图像(空白)
- 传单不会将本地图像显示为叠加
- 切换此元素时,如何使不同的图像显示,而不是文本
- 在页面加载时将图像显示为弹出窗口
- 如何在模糊浏览器主窗口时将图像显示为叠加层
- iOS在画布上绘制图像显示:无会导致内存泄漏
- JavaScript 图像显示不起作用