How to display images in cshtml page after joining two path and converting to base64 in mvc?

471 views Asked by At

This is my controller.

ImagePath is the path got from database and blobUrl is the azure storage path. And I'm joing these two path and converting that in 64base string. But how to pass that 64base value resultimg to Base64string again in above validationdata?. As I'm not able to access image directly for displaying. So required to convert in 64base.

How to pass resultimg value to Base64string???

[HttpGet]
public JsonResult GetImageList(int TaskID)
{
 try
 {
   List<Validation> validationdata = new List<Validation>();
   validationdata = (from d in _context.ImgTable
                where (d.Taskid == TaskID )
                select new Validation
                {
                    d.ImageID,
                    d.Taskid,
                    d.ImagePath,
                    blobUrl = blobContainer.Uri.AbsoluteUri,
                    Base64string  =???
                    
                }).ToList();
                
    //Joining two path and converting it to base64          
    List<string> ImgByteUrl = new List<string>();
    foreach (var item in validationdata.ToList())
    {
        var imgID = item.ID;
        var storagepath = item.blobUrl;         //azure storage url
        var imgurl = item.ImagePath.Trim();     // db image path
        var img1 = img.Split('\\');
        imgurl = (storagepath + '/' + img1[5]);  //joined both path

        byte[] bytes = (new FileUploadDownload(img)).GetByteStream();  //converting in byte
        string imreBase64Data = Convert.ToBase64String(bytes);
        string resultimg = string.Format("data:image/jpg;base64,{0}", imreBase64Data);
        ImgByteUrl.Add(resultimg);

        //validationdata.Add(resultimg);
    }
    
    var jsonResult = Json(validationdata, JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    return jsonResult;
 }
 catch (Exception ex)
 {
 }

}

Please help out how to go further else guide me with any other way. I want to display images in in html page. I'm selecting Taskid from dropdownlist and on-click of it display related images to that taskid in html table.Below is the js function:

$.ajax({
        type: 'GET',
        url: '@Url.Action("GetImageList")',
        datatype: JSON,
        data: {
                'TaskID': $("#tasklist").val()                //getting selected TaskID from dropdown
        },
        success: function (data) {
                 var row_no = 1;
                 $.each(data, function (i, item) {
            
                + "<td id='ItemId" + row_no + "' >" + item.ID + "</td>"               // displaying image id in html table

                "<td>"
                + "<div><object id='Image" + row_no + "' data=" + item.Base64string +></object>"  // display image for that imageid
                + "</div></td > "
     });

This is the image we get after converting to byte and passing that value to

This is the image we get after converting to byte and passing that value to <object id='Image" + row_no + "' data=" + item.Base64string +>

i.e item.Base64string =data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAEMAAABQCAYAAABCiMhGAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAadEVYdFNvZnR3YXJ........

1

There are 1 answers

0
Jason Pan On BEST ANSWER

You should use img tag.

enter image description here

enter image description here

Sample Code:

<div>
    <img style='display:block; width:100px;height:100px;' id='base64image'                 
    src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQsAAAC9CAMAAACTb6i8AAACNFBMVEX////5+fkAAAD8/Pyurq6UlJSoqKiQkJDExMTz8/OdnZ339/fw8PDf39/r6+vV1dXT09Pj4+N6enrLy8u8vLy3t7fg4OCHh4eioqIxMTGamppzc3Nubm5mZmZISEhhYWFXV1eCgoJPT085OTkPDw8+Pj5bW1slJSX///f/+v8SEhIaGhoqKir///P2///mf4jx8OTs5exbVluwqrDBsra0oqH26ur/9f8rLCLLsLLw3eOUnZZ4gHsmHBpNJSvWbpHCW3bTc33egJPLdpCoVm8eBwBaO0naxNMgKRmntLQ5HCDbf53XXob/e5i0UF732uvMnbDXtcmkaYSwhJ7SVGb3anuQXWyrf46DdYE5JC2Qfob1X23tb4SSTFOoa3ZpTVp2LUFuT03kvcO1dH3CVVvrRFL/gYttQUXda3OaV0LddWTov610AADFQ0DzUkPcRT2uPD/LKjX0YWSPGxTok231kV3PUyjNYEP/dk3FFQDxsanwMjTEaWyxjYv/wKbBajT0gUXhWhXtTiX/alqWMzQ1AACuPVFKFQCZdEH/6KH/xHvrfF37f27TLynOOVBsQi6cWCXDPyvho3CuQCTbKUvZy7a2n4zUopjznZr/u8T53cJkJQm6kHWJX0fdyqaAdEGgknf57rpNMADHrYP6yW7WqV61gDHKljHsvFLklFnmhWLdS17IhnL2mkzx1FSyaBfuJwhhE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pCh8hc+Sd8gaULGgKDkF1RWlold0hW/vZC44mNyU+AaojOSaaYq5IuFFL1KjRuwINs0idPDUikRWsuVi22NHdhSwFWla7/tb7ddyLeNRYcZchZeq8zuUC4CdZKQFwM54fUoBVgyj94kWr+k8UnV9+616PRbAbYrOZMLECrjEYMScLjs7Nh9lzt37oJucCTH9/wdPncZ3coKzPwAAAABJRU5ErkJggg==' />
</div>

Your function:

$.ajax({
    type: 'GET',
    url: '@Url.Action("GetImageList")',
    datatype: JSON,
    data: {
            'TaskID': $("#tasklist").val()                //getting selected TaskID from dropdown
    },
    success: function (data) {
             var row_no = 1;
             $.each(data, function (i, item) {
        
            + "<td id='ItemId" + row_no + "' >" + item.ID + "</td>"               // displaying image id in html table

            "<td>"
            + "<div><img id='Image" + row_no + "' src='data:image/png;base64,iVBORw0KGgo.../> </div></td > "
 });