I have a database that has 3 binary image fields. My question is how can I create a hyperlink to open a new page to display the correct image? I can do it if there is only 1 image but I am not sure how to specify the column name for the image. My DB Columns
are named File1, File2 and File3. I want each image on the view page to hyperlink to Retrieve File view (full view of image) , but I am not sure how to set the hyperlink so it knows which image to display as the ID is the same but they are different fields?
Does that make sense?
Here is my controller page that I want to display the image.
public ActionResult RetrieveFile(int id)
{
byte[] cover = GetFileFromDataBase(id);
if (cover != null)
{
return File(cover, "File/jpg");
}
else
{
return null;
}
}
public byte[] GetFileFromDataBase(int Id)
{
var q = from temp in db.HelpDesks where temp.ID == Id select temp.File1;
byte[] cover = q.First();
return cover;
}
And here is where I am displaying in my view:
@{
if (Model.File1 != null)
{
var File1 = Convert.ToBase64String(Model.File1);
var imgSrc1 = String.Format("data:File/gif;base64,{0}", File1);
<img class="thumb" src='@imgSrc1' ;" />
}
<br />
if (Model.File2 != null)
{
var File2 = Convert.ToBase64String(Model.File2);
var imgSrc2 = String.Format("data:File/gif;base64,{0}", File2);
<img class="thumb" src='@imgSrc2' ;" />
}
<br />
if (Model.File3 != null)
{
var File3 = Convert.ToBase64String(Model.File3);
var imgSrc3 = String.Format("data:File/gif;base64,{0}", File3);
<img class="thumb" src='@imgSrc3' ;" />
}
}
According to your description, I recommend that you use model to dynamically display images when displaying image list information, and add
a tag to the image outer layer to specify jumping pages to display full images.
In @Url.Action, by passing the key field corresponding to your current picture stored in the database as a parameter, in the corresponding controller method, you can get the binary array of the
corresponding picture again through the key value, and display the picture through the same conversion.
public ActionResult Index()
{ // display your datas
Entities2 entity = new Entities2();
List<Employee> customers = entity.Employees.ToList();
return View(customers);
}
public ActionResult ShowImage(int imageId)
{
Entities2 entity = new Entities2();
var customer = (from c in entity.Employees where c.EmplId == imageId select c).FirstOrDefault();
return View(customer);
}
Will this work in my case where it is 1 record in the database? Same ID but 3 image fields? Do I pass the image field as the ID?
No, if you have same Id with these 3 image fields, you should change to another method.
I recommend that you can use ajax in jquery to pass base64 string to another action in the controller, then use
Session in controller to record the src of current image.
After this, you can redirect to another view in ajax success method.
Member
29 Points
127 Posts
Click to display database image - mutiple images
Sep 03, 2019 09:03 PM|Baze72|LINK
I have a database that has 3 binary image fields. My question is how can I create a hyperlink to open a new page to display the correct image? I can do it if there is only 1 image but I am not sure how to specify the column name for the image. My DB Columns are named File1, File2 and File3. I want each image on the view page to hyperlink to Retrieve File view (full view of image) , but I am not sure how to set the hyperlink so it knows which image to display as the ID is the same but they are different fields? Does that make sense?
Here is my controller page that I want to display the image.
And here is where I am displaying in my view:
Any ideas would be great!
EB
Contributor
3710 Points
1043 Posts
Re: Click to display database image - mutiple images
Sep 04, 2019 08:46 AM|Yongqing Yu|LINK
Hi Baze,
According to your description, I recommend that you use model to dynamically display images when displaying image list information, and add a tag to the image outer layer to specify jumping pages to display full images.
In @Url.Action, by passing the key field corresponding to your current picture stored in the database as a parameter, in the corresponding controller method, you can get the binary array of the corresponding picture again through the key value, and display the picture through the same conversion.
Index.cshtml:
ShowImage.cshtml:
Here is the result (in my example, i use three same pictures to test):
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
29 Points
127 Posts
Re: Click to display database image - mutiple images
Sep 04, 2019 12:59 PM|Baze72|LINK
Hi YongQing.
Thanks for the reply. Will this work in my case where it is 1 record in the database? Same ID but 3 image fields? Do I pass the image field as the ID?
Thanks
EB
Contributor
3710 Points
1043 Posts
Re: Click to display database image - mutiple images
Sep 05, 2019 02:03 AM|Yongqing Yu|LINK
Hi Baze,
No, if you have same Id with these 3 image fields, you should change to another method.
I recommend that you can use ajax in jquery to pass base64 string to another action in the controller, then use Session in controller to record the src of current image.
After this, you can redirect to another view in ajax success method.
For more details, you could refer to this link:
Index.cshtml:
Controller:
ShowImage.cshtml:
Here is the result :
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
29 Points
127 Posts
Re: Click to display database image - mutiple images
Sep 06, 2019 05:19 PM|Baze72|LINK
Here is what I have - but clicking on the image does nothing at all:
Contributor
3710 Points
1043 Posts
Re: Click to display database image - mutiple images
Sep 09, 2019 01:40 AM|Yongqing Yu|LINK
Hi Baze,
According to your description,I recommend that you use F12 to see if there is any error messages in the console tab.
Could you give us your ShowImage method in HelpDesk controller in detail, which will help us solve your issue more easily.
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
29 Points
127 Posts
Re: Click to display database image - mutiple images
Sep 09, 2019 01:10 PM|Baze72|LINK
I figure it out. I did not have the @model string at the begging of the view like you showed!
Thanks