简体   繁体   中英

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.

我的asp.net的屏幕截图

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.

//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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM