上传图像/文件并将其转换为mysql数据库

时间:2012-04-26 20:15:35

标签: asp.net-mvc-3 image entity-framework-4.1 sql-server-2008-r2 bytearray

我想将图像/文件保存到asp.net mvc3项目的sql-database中。我找到了一些例子,他们不得不将图像保存到数据库中。当我想用我的Controller中的“Create”方法在我的数据库中添加一个“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; }
 }
}

1 个答案:

答案 0 :(得分: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数据库中保存文件。对不起,如果我错误地理解你的问题