使用c#将图像插入数据库

时间:2016-12-24 13:34:05

标签: c# model-view-controller upload insert-image

我想将图像上传到数据库(ShoppingItems)或将图像保存到项目中的文件夹中,并将图像的路径插入到数据库中。有人可以帮忙吗?这是我的代码(视图):

@using (Html.BeginForm("Index3", "Upload", FormMethod.Post))
{
    @Html.TextBoxFor(x => x.Itemname, new { @class = "form-control", placeholder = "Item name", required = "required" })
    <br />

    @Html.TextBoxFor(x => x.Price, new { @class = "form-control", placeholder = "Item price", required = "required" })
    <br />

    @Html.TextBoxFor(x => x.Quantity, new { @class = "form-control", placeholder = "Item quantity"})
    <br />

    @Html.TextBoxFor(x => x.AuthorIdentity, new { @class = "form-control", placeholder = "Username", required = "required" })
    <br />

    // THIS IS WHERE MY IMAGE UPLOAD SHOULD BE  
    <br />

    @Html.TextBoxFor(x => x.Category, new { @class = "form-control", placeholder = "Item category", required = "required" })
    <br />

    @Html.TextAreaFor(x => x.Description, new { @class = "form-control", placeholder = "Item description", required = "required" })
    <br />

    <input type="submit" class="btn btn-danger" value="Add" />
}

控制器:

public ActionResult Index3(ShoppingItem formModel);
{
    using (var ctx = new GikGamerModelDataContext())
    {
        if (formModel == null)
            return View();

        ctx.ShoppingItems.InsertOnSubmit(formModel);
        ctx.SubmitChanges();
    }

    return View();
}

我的上传索引(Index3)只显示上传成功或不成功的文字,所以我没有添加:)

1 个答案:

答案 0 :(得分:0)

在表单中为了上传你必须指定属性enctype:值为“multipart / form-data”  您可以参考此回复中的示例进行尝试:Uploading/Displaying Images in MVC 4

希望有所帮助