开发者

image upload and preview in mvc 2

i am learing mvc 2 by converting a asp.net website.in my page i have to upload a image and show the preview of image.

screen shoot of my asp.net page is given below.

image upload and preview in mvc 2

i have created the model as

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; }
    }

in my controller

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

if i am using iframe in my view to upload image.then how can i bind the file name to contestimage.(i am saving contestimage to database). is there any other method to upload image.


You can simply use <img src= ...> to preview image

Here is an example :- http://weblogs.asp.net/imranbaloch/archive/2010/04/03/image-preview-in-asp-net-mvc.aspx

Upload image to database and preview it: http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/


//in controller u can do this

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" );
    }

//put this in view for diplaying //automatically hooks the Actionresult show

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

id it takes from url:http://localhost//page/1

where 1 is the itemid

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜