在mvc 2中上传和预览图像

我通过转换asp.net网站来学习mvc 2.在我的页面上我必须上传图像并显示图像的预览。

我的asp.net页面的屏幕截图如下。

我的asp.net的屏幕截图

我已经将模型创建为

public class Contest { public int contestid { get; set; } public string ContestName { get; set; } public string Description { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public int UserId { get; set; } public bool IsActive { get; set; } public string contestimage { get; set; } } 

在我的控制器中

 public ActionResult Createcontest() { ContestModel contest = new ContestModel(); return View(contest); } [HttpPost] public ActionResult Createcontest(ContestModel contest) { ///inserting data return View(contest); } 

如果我在我的视图中使用iframe上传image.then如何将文件名绑定到contestimage。(我正在保存对数据库的contestimage)。 还有其他方法上传图片。

//在控制器中你可以做到这一点

 public ActionResult Show( int id ) { byte[] Filecontent1 = null; foreach (byte[] Filecontent in db.ExecuteStoreQuery ("select Filecontent from [barcodes] where Barcode_Id = @p0 ", id)) { Filecontent1 = Filecontent; } var imageData = Filecontent1; return File( imageData, "image/jpg" ); } 

//将其放入视图中进行diplaying //自动挂钩Actionresult节目

  

它来自url:http:// localhost // page / 1

其中1是itemid

您只需使用来预览图像

这是一个例子: – http://weblogs.asp.net/imranbaloch/archive/2010/04/03/image-preview-in-asp-net-mvc.aspx

将图像上传到数据库并进行预览: http : //byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/