将输入文件Ajax发送到ASP.NET MVC控制器

时间:2020-11-08 10:32:43

标签: c# ajax asp.net-mvc

代码ajax

$.ajax({
    type: "POST",
    url: "/Admin/AddItem",
    data: {
        title: title.value, desc: desc.value, quantity: quantity.value, brand: brand.value, cat: cat.value,
        certify: certify.value, gold: gold.value, gold_wt: gold_wt.value, stone: stone.value, stone_wt: stone_wt.value,
        dim: dim.value, dim_wt: dim_wt.value, other: other.value, image: image.files[0], price: price
    },
    dataType: "json",
    success: function (response) {
        alert('Succes!'); window.location.href = '/Admin/Item';
    }
});

image.files[0] =>控制器获取HttpPostedFileBase image

代码控制器

public void AddItem(String title, String desc, int quantity, int brand, int cat, int certify, int gold, float gold_wt,
                    int stone, float stone_wt, int dim, float dim_wt, float other, HttpPostedFileBase image,float price)
{
    String path = "";

    if (image != null)        
    {
        path = Path.Combine(Server.MapPath("~/Content/Products"), Path.GetFileName(image.FileName));
        image.SaveAs(path);
    }
}

我想将图像从Ajax传递到控制器。我怎么弄出来的?

0 个答案:

没有答案