Javascript ASP.NET WebApi POST无法识别表单URL编码的int数组
因此,post给出了如何发送post以使用表单下载文件的答案。但是我的API端点无法识别表单数据Javascript ASP.NET WebApi POST无法识别表单URL编码的int数组,javascript,c#,file,post,asp.net-web-api,Javascript,C#,File,Post,Asp.net Web Api,因此,post给出了如何发送post以使用表单下载文件的答案。但是我的API端点无法识别表单数据 [HttpPost] public async Task<IHttpActionResult> DownloadPdfs([FromBody] IEnumerable<int> ids) { if (ids == null || ids.Count() < 1) { return BadRequest("No ids supplied."
[HttpPost]
public async Task<IHttpActionResult> DownloadPdfs([FromBody] IEnumerable<int> ids)
{
if (ids == null || ids.Count() < 1)
{
return BadRequest("No ids supplied.");
}
...
[HttpPost]
公共异步任务下载PDF([FromBody]IEnumerable ID)
{
if(ids==null | | ids.Count()<1)
{
返回错误请求(“未提供ID”);
}
...
“ids”的计数始终为0
以下是JS代码:
function downloadFile(ids) {
var win = 'w' + Math.floor(Math.random() * 1000000000000);
window.open('', win, 'width=250, height=100');
var f = $('<form></form>')
.attr({ target: win, method: 'post', action: 'api/pdfs' })
.appendTo(document.body);
_.each(ids, function (id) {
$('<input></input>')
.attr({ type: 'hidden', name: 'ids', value: id })
.appendTo(f);
});
f[0].submit();
f.remove();
}
函数下载文件(ids){
var win='w'+Math.floor(Math.random()*10000000000);
窗口打开(“”,win,'宽度=250,高度=100');
变量f=$('')
.attr({target:win,方法:'post',操作:'api/pdfs'})
.附录(文件正文);
_.每个(id、功能(id){
$('')
.attr({type:'hidden',name:'ids',value:id})
.附于(f)之后;
});
f[0]。提交();
f、 删除();
}
最初,我只是将其与另一个链接答案相同,它只是附加到表单中的一个输入,在该表单中,我将值设置为“ids”。我下一步尝试了这个,但仍然不起作用
是否有人知道如何调整这一点,以便仍然使用post数据来提供我的ID,这些ID是创建和提供下载文件所必需的?或者是否有更好的方法来做到这一点
我曾经尝试过使用Ajax,它在responseText中返回文件内容,但是我无法在那里使用它,也无法让浏览器打开它
编辑1
-
我还尝试将该值设置为:
value:'{ids:['+ids.toString()+']}'
那没用。
-
刚刚尝试将endpoint参数更改为:
([FromBody]int[]ids)
…毫无用处。