2016-12-11 48 views
2

クライアントがキッチンの画像をアップロードして、それぞれにテキスト+タイトルを追加したいページがあります。今、私のソリューションはURL経由で画像をアップロードすることをサポートしていますが、クライアントが自分の画像を「参照」ボタンを使ってサイトにアップロードできるようにしたいと考えています。ASP.NET MVCのWebサイトに画像をアップロードしてデータベースに保存する

これは私の現在のモデルクラスである:私はボタンはモデルコントローラの「作成」ビューで、現在置かれていどこ

public class Udstillingsmodel 
{ 
    public int ID { get; set; } 
    public string titel { get; set; } 
    public string beskrivelse { get; set; } 
    public string billedeSti { get; set; } 
} 

public class UdstillingsmodelDBContext : DbContext 
{ 
    public UdstillingsmodelDBContext() : base("UdstillingsmodelDBContext") { } 
    public DbSet<Udstillingsmodel> Udstillingsmodels { get; set; } 
} 

これは、次のとおりです。

<div class="form-group"> 
     <div class="control-label col-md-2"> 
      <p><b>Billede:</b></p> 
     </div> 
     <div class="col-md-10"> 
      <input type="file" accept="image/png, image/jpeg, image/gif" name="image" /> 
      @Html.EditorFor(model => model.billedeSti, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.billedeSti, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

これらの2つです作成機能のためのメソッドをGETとPOST:

// GET: Udstillingsmodels/Create 
public ActionResult Create() 
    { 
     return View(); 
    } 

    // POST: Udstillingsmodels/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "ID,titel,beskrivelse,billedeSti")] Udstillingsmodel udstillingsmodel) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Udstillingsmodels.Add(udstillingsmodel); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(udstillingsmodel); 
    } 

私は「ブラウズ」のお尻を経由して、選択した画像を持っています名前付きの "イメージ"を変数として使用していますが、これをどうしたらいいのか、Entityフレームワークでイメージを正しく保存する方法がわかりません。今私のモデルの私の "イメージ"プロパティは単なる文字列ですが、サイト上の他の場所で行ったように、私の他のすべてのイメージが格納されているコンテンツフォルダの場所を指すことができると考えていました。より多くの情報とコードが必要な場合はお知らせください。

これは、ボタンが追加された状態で「作成」ビューが現在どのように表示されるかを示しています。 http://i.imgur.com/PgYq9nL.png

答えて

2

最初の最初のもの、あなた、あなたのコントローラのは

//"image" is the name of the html fileupload element 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Udstillingsmodel udstillingsmodel,HttpPostedFileBase image) 
    { 
    if (ModelState.IsValid) 
    { 
//upload image 
      if (image!= null && image.ContentLength > 0) 
      { 
       try 
       { 
//Here, I create a custom name for uploaded image 
        string file_name = udstillingsmodel.titel + Path.GetExtension(image.FileName); 

        string path = Path.Combine(Server.MapPath("~/Content/images"), file_name); 
        image.SaveAs(path); 

// image_path is nvarchar type db column. We save the name of the file in that column. 
        udstillingsmodel.image_path= file_name; 
       } 
       catch (Exception ex) 
       { 

       } 
      } 


     db.Udstillingsmodels.Add(udstillingsmodel); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(udstillingsmodel); 
} 

はそれが

+0

を役に立てば幸いアクションを作成し、フォームがこの

@using (Html.BeginForm("youraction", "Your controller", null, FormMethod.Post, new { enctype = "multipart/form-data" })) 

ようにする必要があり始めるあなたの答えをいただき、ありがとうございます。最初のコード行をどこに置いていいのかわかりません。これは、私の "Create"ビューでの画像アップロードのための私の現在のコードを置き換えるものですか?私はHtml.BeginForm()メソッドに慣れていません。 – Leth

+0

作成ビューと編集ビューには@using(Html.BeginForm())コードが必要です。 Create.cshtmlとEdit.cshtml。私の最初のコード行に置き換えてください。 – oneNiceFriend

+0

ああ、今私はそれを参照してください、ありがとう。コントローラでは、ビューの "image"変数を使用するためにメソッドヘッダに必要なパラメータは何ですか? – Leth

関連する問題