1

イメージ/ファイルをasp.net mvc3プロジェクトのSQLデータベースに保存したいと思います。私はいくつかの例を見つけました。そこでは、画像をデータベースに保存する必要がありません。私のコントローラで "Create"メソッドを使用してデータベースに "Inzending"インスタンスを追加する場合、ファイルをバイト配列にする必要があります。フィールドがnullになることはないので、同時にすべての情報をデータベースに挿入する必要があります。私は誰かがこれで私を助けてくれることを願っています。私は学校でそれを始めるばかりで、これは私が解決しなければならない大きな問題です。私のコードの一部は、私はすでに書いた:イメージ/ファイルをアップロードしてmssqlデータベースに変換する

コントローラー:

[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; } 
} 
} 

答えて

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データベース。申し訳ありませんが、あなたの質問を間違って理解した場合

関連する問題