繁体   English   中英

使用asp.net mvc3将图像上传到数据库

[英]Uploading an image to the database using asp.net mvc3

我创建了一个表格来上传有关广告的详细信息,并且需要上传一张图片。在我所有的努力最后,我仍然在数据库中看到图片列为NULL。如何解决此问题?请注意我是初学者。

视图代码

@using (Html.BeginForm("Create", "Advertisement", FormMethod.Post,  new { enctype = "multipart/form-data" })) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Advertisement</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.OwnerID)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OwnerID)
        @Html.ValidationMessageFor(model => model.OwnerID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Date)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Date)
        @Html.ValidationMessageFor(model => model.Date)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Category)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Category,(SelectList)ViewBag.CategoryList)
        @Html.ValidationMessageFor(model => model.Category)
    </div>

    **<div class="editor-label">
        @Html.LabelFor(model => model.Image)
    </div>
    <div class="editor-field">
        <input type="file" id="MyFile" runat="server" />
        @Html.ValidationMessageFor(model => model.Image)
    </div>**

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>


    <p>
        <input type="submit" value="Post"  />
    </p>
</fieldset>


 }

表格如下所示 在此处输入图片说明

在我的模型中,我对Image属性使用“ byte []”类型。在sql数据库中,对image列使用Varbinary(max)类型。

数据库表中的代码

CREATE TABLE [dbo].[Advertisements] (
[ID]          INT             IDENTITY (1, 1) NOT NULL,
[Title]       NVARCHAR (100)  NOT NULL,
[OwnerID]     INT             NOT NULL,
[Date]        DATETIME        NOT NULL,
[Category]    NVARCHAR (MAX)  NOT NULL,
[Image]       VARBINARY (MAX) NULL,
[Description] NVARCHAR (200)  NOT NULL,
CONSTRAINT [PK_dbo.Advertisements] PRIMARY KEY CLUSTERED ([ID] ASC)


  );

控制器动作

[HttpPost]
[Authorize]
public ActionResult Create([Bind(Exclude = "ID")]Models.Advertisement advertisement) //submits the result
{

    if (ModelState.IsValid)
    {
        var db = new AdvertisementDataContext();
        db.Advertisements.Add(advertisement);
        db.SaveChanges(); //saved to the database

        return RedirectToAction("Index");
    }

    return Create(); // if there was an error show the form again

}

使用HttpPostedFileBase将文件作为参数ala发送到控制器: MVC 3文件上传和模型绑定

那么您需要将该参数转换为字节数组并按您的方式保存它(对不起,电话上的文本会很快尝试编辑)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM