2011-05-12 122 views
3

我通過轉換一個asp.net網站來學習mvc 2。在我的頁面中,我必須上傳圖像並顯示圖像預覽。圖像上傳和預覽在mvc 2

我的asp.net頁面的屏幕截圖如下。

Screen shoot of my 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我怎麼綁定文件名稱(我正在將數據庫保存到數據庫)。 有沒有其他的方法來上傳圖片。

+0

你可能想了解MVC3,因爲它是最新版本的atm – Omu 2011-05-12 18:28:55

回答

1

//在控制器u能做到這一點

public ActionResult Show(int id) 
    { 


     byte[] Filecontent1 = null; 


     foreach (byte[] Filecontent in db.ExecuteStoreQuery<byte[]> 
     ("select Filecontent from [barcodes] where Barcode_Id = @p0 ", id)) 
     { 
      Filecontent1 = Filecontent; 
     } 

     var imageData = Filecontent1; 

     return File(imageData, "image/jpg"); 
    } 

//將此視圖置於垂直方向 //自動掛鉤的ActionResult顯示

<tr><img src='<%= Url.Action("Show", "contollername",new {id = Model.itemid }) %>' /></tr> 

ID從URL需要:HTTP://本地主機//頁/ 1

其中1是的itemid