【问题标题】:Insert image into database with c#使用c#将图像插入数据库
【发布时间】:2017-05-09 21:14:16
【问题描述】:

我想将图像上传到数据库 (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) 只显示说明您的上传成功或不成功的文字,所以我没有添加它:)

【问题讨论】:

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


    【解决方案1】:

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

    希望对你有帮助

    【讨论】:

    • 非常感谢。我完全忘记了“多部分/表单数据”部分:) 顺便说一句圣诞快乐
    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多