2014-02-24 22 views
2

我的模型MVC图为byte []

partial class Company 
{ 
    ... More Properties 
    public HttpPostedFileBase FilePicture { get; set; } 
    public byte[] Picture { get; set; } 
} 

我的控制器

[HttpPost] 
public ActionResult Edit(int id, Models.Company model) 
{ 
    if(model.FilePicture != null) 
    { 
     using(Stream inputStream = model.FilePicture.InputStream) 
     { 
      MemoryStream memoryStream = inputStream as MemoryStream; 
      if(memoryStream == null) 
      { 
       memoryStream = new MemoryStream(); 
       inputStream.CopyTo(memoryStream); 
      } 
      model.Picture = memoryStream.ToArray(); 
     } 
    } 
    //EditDefault does the persisting 
    return this.EditDefault(id, model); 
} 

我查看

@using (Html.BeginForm("Edit", currentController, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    //Clicks on the Picture and the Upload butten are forwarded to the file input tag 
    //readUrl sets the image as sone as the file changes 
    <input id="filePicture" type="file" name="filePicture" onchange="readURL(this);"> 

    <button type="button" id="pictureUploadBtnPicture" onclick="$('#filePicture').click();"> Upload</button> 

    //ClearImg clears the img tag and resets the file input tag 
    <button type="button" id="pictureDeleteBtnPicture" onclick="clearimg(this);"> Delete</button> 

    ...if Picture not null 
    ...{ 
     <img id="PicturePicture" src="data:image/png;base64,@System.Convert.ToBase64String(@Model.Picture.ToArray())"> 

     <input type="hidden" value="@System.Convert.ToBase64String(@Model.Picture.ToArray())" name="Picture"> **added** 
    ...} 
    ...else ... Render empty Picture and set it per javascript 

    <input type="submit" value="Safe" class="btn btn-primary"> 
} 

我有一个包含类似名称的一些属性窗体,城市,。 ..和一个包含图片数据的字节[]。上传,显示和删除正在工作。我现在的问题是,当我改变一些东西,并且我再次安全了网站时,模型中的图片属性为null,我在Post Action中获得。我想有一些东西与映射无关。

只是想清楚我想img映射到字节[]。

THX提前任何帮助:)

更新:

THX马特·泰伯;)

增加了一个隐藏的输入字段,现在我明白了在控制器中。

更新了视图,以防有人需要它。

回答

1
<img id="Picture" name="Picture" src="data:image/png;base64,@System.Convert.ToBase64String(@Model.Picture.ToArray())"> 

此属性不是输入,当您提交此服务器它不会sumbit图像。你将不得不使用像隐藏输入存储字节数组。图像标记下

尝试添加该

@Html.HiddenFor(m=>m.Picture)