lundi 28 décembre 2015

Post Form Data by [FromBody] in web api by jquery ajax

I have jquery code to create formData :

        var formData = new FormData();
        formData.append('ID', productID);
        $.each($('body').find('div.addMobile_photo input[name="photoSelect"]')[0].files, function (i, file) {
            data.append('file-' + i, file);
        });

and ajax like this :

        $.ajax({
            cache: false,
            url: home.url + 'admin/Mobile_Add/mobile/addphoto',
            data: { '': list},
            type: 'POST',
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (data) {
                // .... do something
            }
        });

this is actually my problem how to post formData to web api, and my web api code like it :

    [HttpPost]
    [ActionName("mobile")]
    [Route("api/admin/Mobile_Add/mobile/addphoto")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public string MobileAddPhoto([FromBody]NameValueCollection List, string ID)
    {
        foreach (var key in List.AllKeys)
        {
            var file = HttpContext.Current.Request.Files[key];
            file.SaveAs(HttpContext.Current.Server.MapPath("/product/mobile/" + ID + "/" + file.FileName));

        }
        return "1";
    }

this is problem how can i send formData by [FromBody] or [FromUri] ?




Aucun commentaire:

Enregistrer un commentaire