Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

c# - How to upload/display images using ASP.net MVC4 with Entity Framework

I have a data model like so

    public class NewsItem
{
    public virtual int Id { get; set; }
    public virtual string NewsTitle { get; set; }
    public virtual string NewsContent { get; set; }
    public virtual byte[] NewsImage { get; set; }
    public virtual DateTime DateAdded { get; set; }
    public virtual bool IsLive { get; set; }
}

I then display this data through a View like so:

@model BusinessObject.Domain.NewsItem
<div class="row-fluid">
    <h3>
        @Html.ValueFor(model => model.NewsTitle)
    </h3>
    <div class="span5">
    <img src="~/Content/images/stock.jpg" />
    </div>

<div class="span7">
<p>
    @Html.ValueFor(model => model.DateAdded)
</p>
<p>
    @Html.ValueFor(model => model.NewsContent)
</p>
</div>
 </div>

I then save the data using the _db.SaveChanges() in my controller like so:

[Authorize]
    [HttpPost]
    public ActionResult Create(CreateNewsViewModel input)
    {
        if (ModelState.IsValid)
        {
            var news = new NewsItem();
            news.NewsTitle = input.nTitle;
            news.NewsContent = input.nContent;
            news.DateAdded = input.nDateAdded;
            news.IsLive = input.nIsLive;
            Mydb data = new Mydb();
            data.NewsItems.Add(news);
            data.SaveChanges();
            return View("Index", data.NewsItems);

        }
        else
        {
            return View(input);
        }
    }

Currently I don't have an image upload bit. How would I go about this? In my db I have a binary field, and my data type in my object is a byte[]. But I don't know where I need to handle the Image Upload?

Do I need a seperate action that returns the view? Some pointers on this would be grand.

Cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll want an input field of type file to upload from the View and an instance of the WebImage to handle the uploaded image:

View:

<input type="file" name="image" />

Controller:

WebImage image = WebImage.GetImageFromRequest();
byte[] toPutInDb = WebImage.GetBytes();

// ... put the byte array into the database

To display the images from your database, you will want a controller Action that retrieves the byte array from your database and returns a FileAction. You can get this image (for the image retrieving controller action) by instantiating another WebImage instance on the bytearray you retrieve from database:

WebImage image = new WebImage(byteArrayFromDb);

File(image.GetBytes(), "image/" + image.ImageFormat, image.FileName);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...