0

Hi Guys I am trying to Display the images from database I save the images in the database but it not display any thing how can i fix this here in my Code

this is my model view

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Archive.Models
{
    public class Inbox
    {
        [Key]

        public int Id { get; set; }

        public IEnumerable<HttpPostedFileBase> Files { get; set; }


        public ApplicationUser User { get; set; }
        public object UserId { get; internal set; }
    }
}

and here details

    namespace Archive.Models
{
    public class AllFiles
    {
        public int Id { get; set; }
        public string ContentType{ get; set; }
        public byte[] Imagebytes { get; set; }
    }
}

here i Convert

using System.Web;
using System.IO;

namespace FileUpload.Service
{
    public class FileUploadService
    {
        // GET: AllFiles
        public void saveFileDetails(HttpPostedFileBase file)
        {
            AllFiles newfile = new AllFiles();
            newfile.ContentType = file.ContentType;
            newfile.Imagebytes = ConvertToBytes(file);
            ApplicationDbContext db = new ApplicationDbContext();
            {
                db.AllFiless.Add(newfile);
                db.SaveChanges();      
            }
        }
        public byte[] ConvertToBytes(HttpPostedFileBase file)
        {
            byte[] imagebytes = null;
            BinaryReader reader = new BinaryReader(file.InputStream);
            imagebytes = reader.ReadBytes((int)file.ContentLength);
            return imagebytes;
        }
    }
}

and here is my Controller

        public async Task<ActionResult> Create(Inbox model)
    {

        var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
        if (ModelState.IsValid)
        {
            model.User = currentUser;

            FileUploadService service = new FileUploadService();
            foreach (var item in model.Files)
            {
                service.saveFileDetails(item);
            }

                var max = new Inbox();

            db.Inboxs.Add(model);
            db.SaveChanges();
            string url = Url.Action("List");
            return Json(new { success = true, url = url });
        }
        return View(model);
    }

and this is my view details

    @foreach(var image in Model)
{
    var base64 = Convert.ToBase64String(image.Files);
    var imgSrc = String.Format("data:{0};base64,{1}",image.ContentType ,base64);
    <img src="@imgSrc"/>
}

i am try to display images here but is not show up what is problem guys please guys i am trying this so hard i asked here but i did't get the images

the image in database it saved like this here

jmal hassn
  • 13
  • 6
  • **How** are those images saved? Blobs in the Database? Files in the FileSystem? Is the folder public/do you got a HTTP handler to serve those images to WebBrowsers? – Christopher Nov 12 '18 at 16:59
  • Where is the GET action method code for the view where you are trying to display the images ? – Shyju Nov 12 '18 at 17:40
  • i am trying to display the images in a inbox controller i don't have a get action to display how can i do that – jmal hassn Nov 12 '18 at 17:47
  • For the request, you need a GET action which gets the data and pass it to the view. Is your current code properly saving the data ? I see many `ApplicationDbContext` objects inside the save code. – Shyju Nov 12 '18 at 18:29
  • Shyju can you help me do this please bro – jmal hassn Nov 12 '18 at 18:51
  • no one really ? – jmal hassn Nov 14 '18 at 13:13

0 Answers0