使用angularjs 1.3和C# .net核心web api
我有一个ng- file -upload,用来上传文件。当upload方法被调用时,我希望将一些数据的额外数组传递给upload方法,然后该方法在我的web api中接收该数组。这是我的ng-file-upload
factory.upload = function (file, myArray) {
var url = '{0}upload'.format(apiPath)
return Upload.upload({
url: url,
arrayKey: '',
data: { file: file, myArray: myArray}
}).then(function (res) {
return res;
});
};
下面是我的webapi:
[HttpPost("upload")]
public async Task<IActionResult> FileUpload(IFormFile file, List<string> myArray)
{
//code
}
最后是我尝试在上传到我的webapi时传递的数组:
[
{
id: "1",
name: "steve"
},
{
id: "2",
name: "adam"
}
]
问题出在我的webapi中,接受来自UI的数组的myArray参数始终为空。我在网上搜索了一下,有提到要添加
arrayKey: ''
但还是不能工作。有什么建议吗?
-更新
我创建一个字符串数组,如下所示:
var cars = ["steve", "adam", "ronnie"];
并将我的api更新为:
List<ArrayItem> myArray
上面的代码可以工作。所以看起来传递的数组有问题。我通过如下所示的方式传递以下数组:
var myArray= [];
for (var i = 0; i < myJson.length; i++) {
myArray.push({
id: myJson[i].id,
name: myJson[i].name,
});
}
以上结果如控制台所示:
Array(2)
0:{id: "1", name: "steve"}
1:{id: "2", name: "adam"}
这里遗漏了什么?
发布于 2018-08-30 11:13:54
为了传递object数组,需要定义接受参数的list对象。
Id
/Name
properties.public类ArrayItem { public int Id { get;set;}公共字符串名称{ get;set;}}更新
您需要删除arrayKey: '[]'
,请尝试以下代码:
app.service('crudService', function ($http, Upload) {
var baseUrl = 'http://localhost:50829';
this.uploadFile = function (file, array) {
return Upload.upload({
url: baseUrl + "/api/books/file/upload",
//arrayKey: '[]',
data: { file: file, array: array },
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function (res) {
return res;
}, function (err) {
throw err;
});
};
});
https://stackoverflow.com/questions/52084641
复制相似问题