Angularjs Ng文件上载:将数组发送到Web Api
使用angularjs 1.3和C#.net核心web api 我有一个ng文件上传,用于上传文件。当upload方法被调用时,我想将一些额外的数据数组传递给upload方法,然后该方法在我的web api上接收这些数据。这是我的ng文件上传Angularjs Ng文件上载:将数组发送到Web Api,angularjs,json,asp.net-web-api,asp.net-core-webapi,ng-file-upload,Angularjs,Json,Asp.net Web Api,Asp.net Core Webapi,Ng File Upload,使用angularjs 1.3和C#.net核心web api 我有一个ng文件上传,用于上传文件。当upload方法被调用时,我想将一些额外的数据数组传递给upload方法,然后该方法在我的web api上接收这些数据。这是我的ng文件上传 factory.upload = function (file, myArray) { var url = '{0}upload'.format(apiPath) return Upload.upload({ url: ur
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
}
[
{
id: "1",
name: "steve"
},
{
id: "2",
name: "adam"
}
]
问题是在我的webapi中,从UI接受数组的myArray参数始终为空。我在网上搜索了一下,上面提到了要添加的内容
arrayKey: ''
但仍然不起作用。有什么意见吗
---更新---
我创建了一个字符串数组,如下所示:
var cars = ["steve", "adam", "ronnie"];
并将我的api更新为:
List<ArrayItem> myArray
这里缺少什么?对于传递对象数组,您需要定义列表对象以接受参数
Id
/名称
属性的数组项
public class ArrayItem
{
public int Id { get; set; }
public string Name { get; set; }
}
public async Task<IActionResult> FileUpload(IFormFile file, List<ArrayItem> myArray)
{
return Ok();
}
我没有确切的答案,但我想指出,您的API方法需要
列表
,但实际上您正在发送它列表
(可能不是“对象”,您可能需要为此id/name对象创建一个接口)。也许这就是它为空的原因吧?@Hankscopio我用这个对象试过了,但仍然不起作用。关于我的ng文件上传,有什么变化吗?我已经尝试了如上更新,但不起作用,仍然为空。angularjs文件上传中是否有需要更新的内容?@user1563677与我们分享web浏览器网络选项卡中请求的屏幕截图。有任何可复制的项目吗?这里是截图:我可以创建一个JSFIDLE,但如何在那里显示/创建我的c#api代码?@user1563677将我的api更新为:List myArray是什么意思?您需要将列表myArray
更改为列表myArray
。很抱歉,这是我这边的输入错误。我已经按照您的指示进行了更新,以使用ArrayItem,但它不起作用。仍然是空的
public class ArrayItem
{
public int Id { get; set; }
public string Name { get; set; }
}
public async Task<IActionResult> FileUpload(IFormFile file, List<ArrayItem> myArray)
{
return Ok();
}
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;
});
};
});