无法使用Ajax在Web Api中发送文件/图像

时间:2019-02-16 06:08:33

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

您好,我正在使用post方法在Web Api中发送对象集合 但问题是发布后接收了所有数据,但文件为空,我的数据如下所示

[{"Name":"Shyju","Id":123,"Tags":[{"Id":12},{"Id":12},{"Id":12}, 
{"Id":12}],"File":{}}
,
{"Name":"Shyju","Id":123,"Tags":[{"Id":12},{"Id":12}, 
{"Id":12},{"Id":12}],"File":{}}
,
{"Name":"Shyju","Id":123,"Tags":[{"Id":12}, 
{"Id":12},{"Id":12},{"Id":12}],"File":{}}]

我的viewmodel类是

public class CreateUserViewModel
{
    public int Id { set; get; }
    public string Name { set; get; }
    public List<TagViewModel> Tags { set; get; }
    public List<ImageFile> File { set; get; }
}
public class TagViewModel
{
    public int Id { set; get; }

}

public class ImageFile
{
    public HttpPostedFileBase File { set; get; }

}

和控制器

 public Result PostDescription(List<CreateUserViewModel> model)
    {
     //Logics to save data
    }

Jquery代码是

 var model = [];
        var tag = [];
        var data = new FormData($('form')[0]);


        for (var i = 0; i <= 3; i++) {

            tag.push({
                "Id": 12
            });

        }

        for (var i = 0; i <= 3; i++) {
            model.push({
                "Name": "Shyju",
                "Id": 123,
                "Tags": tag,
                "File": data
            });
            $.ajax({
                type: "POST",
                data: model,
                url: "/api/BrandIssue",
                contentType: "application/json"
            }).done(function(res) {

            });
        }

我无法处理文件上传以及其他数据。请在此问题上获得帮助

0 个答案:

没有答案