1

我想将图像/文件保存到 asp.net mvc3 项目中的 sql 数据库中。我发现了一些示例,他们必须将图像保存到数据库中而没有别的。当我想使用控制器中的“创建”方法在我的数据库中添加一个“Inzending”实例时,我还需要将文件放入一个字节数组中。我必须同时将所有信息插入到我的数据库中,因为没有字段可能为空。我希望有人可以帮助我,我刚从学校开始,这是我必须解决的第一个真正的大问题(我觉得很难解决)。我已经写过的一些代码:

控制器:

[HttpPost]
    public ActionResult Create(INZENDING inzending)
    {

        string user = User.Identity.Name;
        var users = db.aspnet_Users.Single(p => p.UserName == user).UserId;
        inzending.Userid = users;

        int id = db.INZENDINGs.Count() + 1;
        inzending.InzendingNr = id;
        if (ModelState.IsValid)
        {

            db.INZENDINGs.Add(inzending);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }
        ViewBag.ErfgoedNr = new SelectList(db.ERFGOEDFICHEs, "ErfgoedNr", "Naam", inzending.ErfgoedNr);
        ViewBag.Aard = new SelectList(db.INPUT_AARD, "Aard", "Aard", inzending.Aard);
        ViewBag.Type = new SelectList(db.INPUTTYPEs, "type", "type", inzending.Type);
        ViewBag.Status = new SelectList(db.STATUS, "Waarde", "Waarde", inzending.Status);
        return View(inzending);
    }

看法 :

 @model Erfgoed.Models.INZENDING

 @{
ViewBag.Title = "Create";
 }

<h2>Create</h2>

  <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>INZENDING</legend>

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

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

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.Type, "INPUTTYPE")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Type", String.Empty)
        @Html.ValidationMessageFor(model => model.Type)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Aard, "INPUT_AARD")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Aard", String.Empty)
        @Html.ValidationMessageFor(model => model.Aard)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ErfgoedNr, "ERFGOEDFICHE")
    </div>
    <div class="editor-field">
        @Html.DropDownList("ErfgoedNr", String.Empty)
        @Html.ValidationMessageFor(model => model.ErfgoedNr)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.bestand, "Bestand");
     </div>

</fieldset>

 }
 <div>
     @Html.ActionLink("Back to List", "Index")
 </div>

模型:

namespace Erfgoed.Models
{
using System;
using System.Collections.Generic;

public partial class INZENDING
{
    public int InzendingNr { get; set; }
    public string Titel { get; set; }
    public string Auteur { get; set; }
    public System.DateTime Datum { get; set; }
    public string bestandsgrootte { get; set; }
    public string Type { get; set; }
    public string Aard { get; set; }
    public byte[] bestand { get; set; }
    public System.Guid Userid { get; set; }
    public int ErfgoedNr { get; set; }
    public string Status { get; set; }

    public virtual aspnet_Membership aspnet_Membership { get; set; }
    public virtual ERFGOEDFICHE ERFGOEDFICHE { get; set; }
    public virtual INPUT_AARD INPUT_AARD { get; set; }
    public virtual INPUTTYPE INPUTTYPE { get; set; }
    public virtual STATUS STATUS1 { get; set; }
 }
}
4

1 回答 1

0
    [HttpPost]
    public ActionResult Create(HttpPostedFileBase file, FormCollection collection)
    {
        var attachment = new Attachment();
        if(TryUpdateModel(attachment))
        {
            long length =
                file.InputStream.Length;
            attachment.Data = new byte[length];
            file.InputStream.Read(attachment.Data, 0, (int)length);
            attachmentRepository.Add(attachment);
            attachmentRepository.Save();

            return RedirectToAction("Index");
        }

        return null;
    }

这是我在 ms sql 数据库中保存文件的方式。对不起,如果我错误地理解了你的问题

于 2012-04-26T21:04:20.793 回答