简体   繁体   中英

Displaying only uploaded images

Controller:

[HttpPost]
public ActionResult UploadImage(List<HttpPostedFileBase> files)
{
    EntityDBContext db = new EntityDBContext();
    var imagesShown = db.Images.ToList();

    foreach (var file in files)
    {
        if (file != null)
        {
            string imageName = Path.GetFileName(file.FileName);
            string physicalPath = Server.MapPath("~/Images/" + imageName);
            byte[] pictureAsBytes = new byte[file.ContentLength];

            using (BinaryReader br = new BinaryReader(file.InputStream))
            {
                pictureAsBytes = br.ReadBytes(file.ContentLength);
            }

            //Save to folder
            file.SaveAs(physicalPath);

            //Save new record in database
            Image img = new Image();

            img.ImageName = imageName;
            img.ImageBytes = pictureAsBytes;

            db.Images.Add(img);
            db.SaveChanges();

            //Only show images that user uploaded, not entire database
            imagesShown = imagesShown.Where(x => x.ImageName == imageName).ToList();
        }
        else
        {
            return View("Index");
        }
    }

    return View("ShowImage", imagesShown);
}

View:

@model IEnumerable<Vidafo.Models.Image>

@foreach(var image in Model)
{
    <img src="data:image; base64, @System.Convert.ToBase64String(image.ImageBytes)" />
}

I have tried to show only images user have uploaded with the IQueryable line, however this isn't good because if the name is already in the database it will post all of them.

I only want the user photo's that they just uploaded to be shown. Not the whole database, how do I do this? I presume I have to use 'file' somehow but I am not sure how.

The following snippet results in only a single save and returns only the newly uploaded images (I haven't tested your file upload code, so assuming it is functioning correctly):

[HttpPost]
public ActionResult UploadImage(List<HttpPostedFileBase> files)
{
    EntityDBContext db = new EntityDBContext();
    List<Image> uploadedImages = new List<Image>();

    foreach (var file in files)
    {
        if (file != null)
        {
            string imageName = Path.GetFileName(file.FileName);
            string physicalPath = Server.MapPath("~/Images/" + imageName);
            byte[] pictureAsBytes = new byte[file.ContentLength];

            using (BinaryReader br = new BinaryReader(file.InputStream))
            {
                pictureAsBytes = br.ReadBytes(file.ContentLength);
            }

            //Save to folder
            file.SaveAs(physicalPath);

            //Save new record in database
            Image img = new Image
                {
                    ImageName = imageName;
                    ImageBytes = pictureAsBytes;
                };

            uploadedImages.Add(img);
        }
    }
    db.Images.AddRange(uploadedImages);
    db.SaveChanges();
    return View("ShowImage", uploadedImages);
}

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