简体   繁体   English

仅显示上传的图像

[英]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. 我试图仅显示用户使用IQueryable行上传的图像,但这不是很好,因为如果名称已经在数据库中,它将发布所有图像。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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