【发布时间】:2019-03-03 08:48:23
【问题描述】:
我的示例仅适用于一个文件。我需要一组文件。我使用 angularJS 1.7 和 asp.net-core 2.0
HTML
<div class="post">
<form>
<textarea ng-model="Headline" name="Headline" rows="2" cols="63" class="form-control"></textarea>
<textarea ng-model="BodyText" name="BodyText" rows="4" cols="63" class="form-control"></textarea>
<input type = "file" file-model = "myFile" multiple/>
<input id="Submit" class="btn btn-default" ng-click="createPost()" type="submit" value="Пост" />
</form>
</div>
AgularJs 控制器中的方法 creatPost
$scope.createPost = function () {
var model = {
Headline: $scope.Headline,
BodyText: $scope.BodyText,
ImgPost: $scope.myFile
}
$http({
method: 'POST',
url: '/api/Blog/create-post/',
headers: {
'Content-Type': undefined
},
data: model,
transformRequest: function (data, headersGetter) {
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
return formData;
}
})
.then(function onSuccess(response) {
if (response.data = "Ok") {
$window.location.reload();
}
})
.catch(function onError(response) {
console.log("Error")
});
}
C# 中的模型
[ScaffoldColumn(false)]
public int Id { get; set; }
[ScaffoldColumn(false)]
public string User { get; set; }
[Required(ErrorMessage = "")]
[StringLength(100, MinimumLength = 1, ErrorMessage = "")]
public string Headline { get; set; }
[Required(ErrorMessage = "")]
[StringLength(1000, MinimumLength = 1, ErrorMessage = "")]
public string BodyText { get; set; }
[ScaffoldColumn(false)]
public DateTime? Date_Time { get; set; }
public IFormFile ImgPost { get; set; }
还有一个方法签名
[HttpPost]
[Route("create-post")]
public async Task<object> PostSaveOnFile(PostViewModel model)
{
我需要如何在# public IFormFile ImgPost { get; 中更改我的模型放; } 并在我的 angularJS 控制器上进行更改,我应该能够发送一组文件。谢谢。
据我了解,fileReader 指令中存在问题
(function (angular) {
angular.module('app').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]);
});
});
}
};
}]);
})(window.angular);
如果我将 C# 模型更改为 IList 或 IEnumerable,它将只是第一个文件。我不明白如何将我的指令更改为数组
【问题讨论】: