My images are stored in the database in a byte array format, it displays everything perfectly through forich, but when I want to open a page to work with a certain image, it gives an error saying that I am passing a non-byte value, although when I step through the code and watch the transmitted value , it displays byte [values].

@foreach (var item in Model) { <tr> <td> @Html.Raw("<img style='width:80px; height:60px;' src=\"data:image/jpeg;base64," + Convert.ToBase64String(item.Image) + "\" />") </td> <td> @Html.ActionLink("Edit", "Edit", "Personal", new { id = item.Id, item.Image }, null) </td> <td> @Html.ActionLink("Remove", "Delete", "Personal", new { id = item.Id}, null) </td> </tr> } 

in Edit, I pass the image id and its byte array itself. Edit:

 public ActionResult Edit(int id, byte[] image) { ApplicationUser au = db.Users.Find(User.Identity.GetUserId()); Picture pic = new Picture { Id = id, Image = image}; return View(pic); } 

Table structure

 CREATE TABLE [dbo].[Pictures] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Size] NVARCHAR (MAX) NULL, [Image] VARBINARY (MAX) NULL, [UserId] NVARCHAR (MAX) NULL, [ApplicationUser_Id] NVARCHAR (128) NULL, CONSTRAINT [PK_dbo.Pictures] PRIMARY KEY CLUSTERED ([Id] ASC), CONSTRAINT [FK_dbo.Pictures_dbo.AspNetUsers_ApplicationUser_Id] FOREIGN KEY ([ApplicationUser_Id]) REFERENCES [dbo].[AspNetUsers] ([Id]) ); 

Mistake

It seems to me that it is possible to transfer an element of a collection somehow easier, but I cannot select one element on the controller in the controller.

  • I tried using Linq to pull out the request, but all the same I swear at the type public ActionResult Edit(int id, byte[] image) { ApplicationUser au = db.Users.Find(User.Identity.GetUserId()); Picture pic = new Picture(); foreach (var item in au.Pictures) { pic = from p in au.Pictures where p.Id == id select p; } //Picture pic = new Picture { Id = id, Image = image}; return View(pic); } public ActionResult Edit(int id, byte[] image) { ApplicationUser au = db.Users.Find(User.Identity.GetUserId()); Picture pic = new Picture(); foreach (var item in au.Pictures) { pic = from p in au.Pictures where p.Id == id select p; } //Picture pic = new Picture { Id = id, Image = image}; return View(pic); } public ActionResult Edit(int id, byte[] image) { ApplicationUser au = db.Users.Find(User.Identity.GetUserId()); Picture pic = new Picture(); foreach (var item in au.Pictures) { pic = from p in au.Pictures where p.Id == id select p; } //Picture pic = new Picture { Id = id, Image = image}; return View(pic); } - Artem Danchenko
  • one
    It is necessary to transfer not just an array of bytes, but in the BASE64 encoding, which is written in error. In the first fragment you have a conversion to BASE64, and in Edit I do not watch it. - rdorn
  • @rdorn wait, but in BASE64 I translate before displaying the image, and in Edit I need to transfer the byte array of the image, so that on another page I can convert the image by converting the transferred array into Base64. In general, this is all a bit through the ass, but I don `t know how to pull an element from the Pictures collection, so I have to carry out such manipulations with the transfer of the array. - Artem Danchenko
  • Well, you're on http transferring data to the client? http - text format, BASE64 - compatible with http, binary format, all bytes of which have a printed representation, in the form of valid display characters. - rdorn
  • @rdorn it is so, but gives an error message for too long URL - Artem Danchenko

1 answer 1

And why to transfer through url to the image ?? just transfer the image id and get id from the database. it would be better to create some kind of DownloadPicture Action type (imageId) and which will return to the image or base64 and could then be used

 <img src="DownloadPicture(imageId)"/>