【发布时间】:2016-12-09 16:48:17
【问题描述】:
我正在使用 Angular 调用 ASP.NET Web API 端点,该端点接受包含客户记录的文件并将它们导入数据库。导入工作正常,但我无法弄清楚如何向用户显示导入结果。我想列出每条导入的记录并显示它是否成功。
+----------+-----------+---------------+
| LastName | FirstName | Import-Status |
+----------+-----------+---------------+
| Smith | Tom | Success |
+----------+-----------+---------------+
| Manning | Pam | Failure |
+----------+-----------+---------------+
| Jenkins | Alan | Success |
+----------+-----------+---------------+
| Zip | George | Failure |
+----------+-----------+---------------+
为此,我从 Web Api 返回了一组结果..
...
List<ImportResults> results = ProcessCustomerFile.ReadFile(uploadedFileName);
return Request.CreateResponse<IEnumerable<ImportResult>>(HttpStatusCode.OK, results);
这是处理返回的 Angular 函数
$scope.uploadFiles = function () {
uploadService.uploadFiles($scope)
// then() called when uploadFiles gets back
.then(function (data) {
// promise fulfilled
if (data.length === 0) {
alert("File Uploaded and Processed")
$scope.formdata = new FormData();
$scope.data = [];
$scope.$apply;
} else {
// There is data being returned so assign to the $scope variable
alert("File Uploaded and Processed with returned data");
$scope.resultsCollection;
$scope.$apply;
alert(data);
}
}, function (error) {
//Server Error
$scope.uploading = false;
alert("Function error " + error);
}
);
};
我遇到的问题是 AngularJS 代码似乎将返回的数据视为错误。我正在导入四个记录,因此由于消息中有四个对象,它看起来是我想要的数据。我只是不确定为什么function (error) {} 会处理它。
更新:添加 uploadService 的代码
.factory('uploadService', function ( $http, $q) {
return {
uploadFiles: function ($scope) {
var request = {
method: 'POST',
url: 'http://localhost:4321/api/customerupload',
data: $scope.formdata ,
headers: {
'Content-Type': undefined
}
};
// SEND THE FILES.
return $http(request)
.then(
function (response) {
if (typeof response.data === 'string') {
return response.data;
} else {
return $q.reject(response.data);
}
},
function (response) {
return $q.reject(response.data);
}
);
}
};
})
【问题讨论】:
-
能否请您包括uploadService的相关部分和uploadFiles的定义?谢谢
标签: c# angularjs asp.net-web-api2