开发者

ASP.Net MVC 2 Image upload

i am trying to do a simple image upload using MVC2. in my view i have:

<% using (Html.BeginForm("Upload","Home")) { %> 
    <input type="file" name="upload" id="ImageUpload" value="Upload Image"/>
    <input type="submit" value="Upload" />
<% } %>

In my controller (Home), how do i get this Image u开发者_JAVA百科ploaded and save it to a database? I am very new to ASP.Net MVC and this thing has got me stuck. Thanks in advance for your help and time.


Edit:

okay, i guess my question is vague from the answer i got to provide more detail , This is what i have:

The image model is simple as below --

public class ImageModel
{
    public Image image;
    public string ImageName;

    public ImageModel(Image image, string name)
    {
        this.image = image;
        ImageName = name;
    }

}

the view is like this:

<%using (Html.BeginForm("Upload","Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{%>    
    <input type="text" id="ImageName" />
    <input type="file" name="upload" id="ImageUpload" value="Upload Image"/>
    <input type="submit" value="Upload" />
<%} %>

the controller is where i want create a new ImageModel instance, validate it and if valid save it to the database: So i have:

    public ActionResult Upload(ImageModel image)
    {
        //this is where i am stuck?
        //how to get the supplied image as part of the ImageModel object
        //whats the best way to retrieve the supplied image

        ImageModel temp = image;
        if(!temp.IsValid()){
            //get errors

            //return error view
        }
        uploadrepository.SaveImage(temp);     

        return View();
    }

The question is how to get the supplied image and save it to the database


based on your View code try changing your model to this...

public class ImageModel
{
    public HttpPostedFileWrapper upload { get; set; }
    public string ImageName { get; set; } 
}

also, you'll need to name that text input element (not just id)...

<input type="text" id="ImageName" name="ImageName" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜