2016-02-28 96 views
0

我剛剛嘗試了很多我發現的東西,但最終沒有任何成功。HttpPostedFileBase varbinary(max)

首先我有下一個代碼,只是做的事情確定,但不保存圖像。

如何將圖像保存到varbinarymax中?以及如何將它們展示給下一個視圖?

觀點:

<div class="form-group"> 
    @Html.LabelFor(model => model.Logo, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
    @*@Html.EditorFor(model => model.Logo, new { htmlAttributes = new { @class = "form-control" } })*@ 
    @Html.TextBoxFor(model => model.Logo, new { type = "file" }) 
    @Html.ValidationMessageFor(model => model.Logo, "", new { @class = "text-danger" }) 
    </div> 
</div> 

控制器:

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school) 
{ 
    if (ModelState.IsValid) 
    { 
    db.School.Add(school); 
    await db.SaveChangesAsync(); 
    return RedirectToAction("Index"); 
    } 
    return View(school); 
} 

型號:

public partial class School 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public School() 
    { 
    this.Product = new HashSet<Product>(); 
    } 

    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string Description { get; set; } 
    public string Mail { get; set; } 
    public int? Phone { get; set; } 
    public byte[] Logo { get; set; } 
    public string Small_Description { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Product> Product { get; set; } 
} 

回答

0

由於你沒有發佈完整的形式,這裏是完整的代碼上傳圖像並保存進入DB。

你表格必須有enctype屬性。

@using (Html.BeginForm("Index","Home",FormMethod.Post, new{ enctype = "multipart/form-data" })) 
{ 
    //your other code 
    <input type="file" name="logo" /> 
    <input type="submit" value="Save" /> 
} 

而且在你的行動。

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school) 
{ 
    if (ModelState.IsValid) 
    { 
     byte[] fileData = null; 
     using (var binaryReader = new BinaryReader(Request.Files["logo"].InputStream)) 
     { 
      fileData = binaryReader.ReadBytes(Request.Files["logo"].ContentLength); 
     } 
    school.Logo=fileData; 
    db.School.Add(school); 
    await db.SaveChangesAsync(); 
    return RedirectToAction("Index"); 
    } 
    return View(school); 
} 

它會將文件保存在您的Db中。

0

更改視圖首先這樣:

@using (Html.BeginForm("Create", "Schole", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
<div class="form-group"> 
    @Html.LabelFor(model => model.Logo, htmlAttributes: new { @class = "control-label col-md-2" }) 
<div class="col-md-10"> 
@Html.TextBoxFor(model => model.Logo, new { type = "file" }) 
    <input type="submit" value="submit" /> 
</div> 
</div> 
} 

變化行動這樣的:

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school, HttpPostedFileBase Logo) 
    { 
     if (ModelState.IsValid) 
     { 
      using (var memoryStream = new MemoryStream()) 
      { 
       Logo.InputStream.CopyTo(memoryStream); 
       school.Logo = memoryStream.ToArray(); 
      } 
      db.School.Add(school); 
      await db.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 

     return View(school); 
    } 
} 

現在這個標誌保存。

相關問題