传递给webApi控制器的图像数组为null

时间:2018-01-18 09:24:57

标签: c# ajax asp.net-web-api

使用ajax将图像传递给webApi控制器很简单并且完成了,但是当我尝试传递图像数组时,images属性的值完全为null。 有人有任何想法吗?

这是我的代码:

型号:

public HttpFile UploadedImages { get; set; }

查看:

<input type="file" id="files" name="files" multiple="multiple">

<script type="text/javascript">
    var picArray = [];
</script>

Js:

picReader.addEventListener("load", function (event) {
    var picFile = event.target;
    picArray.push(picFile.result);
}

formData = new FormData();
formData.append("UploadedImages", picArray);

        var ajaxRequest = $.ajax({
            type: 'POST',
            url: '/api/CRUD/AddGlass/',
            processData: false,
            contentType: false,
            data: formData,
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var requestResponse = {
                    httpStatus: jqXHR.status,
                    error: errorThrown || jqXHR.statusText,
                };
            }
        });

Api:

public string AddGlass(GlassBindingModel model)
{
    // Here model.UploadedImages is null
}

赞赏并感谢任何帮助

1 个答案:

答案 0 :(得分:2)

尝试使用FromBody属性

public string AddGlass([FromBody]GlassBindingModel model)