1

I want to show the picture in the preview after I click on edit button, but it gets a picture in binary form in the list and then passes it to ajax call through Json result after that it did not convert under ajax call.can anyone help me to sort out this problem.thanks

JS

function Edit(id) {        
    $("#submit").html("Update  Record");
    $.ajax({
        type: 'GET',
        url: '/Home/EditUser', 
        data: { userid:id},
        success: function (data) {                
            var obj = JSON.parse(data);
            $("#imgPreview").show();
            $("#imageBrowes:file").html(obj.Image_ID);
            $("#targetImg").attr('src', "data:image/gif;base64,{0}", obj.ImageTbl.ImageByte);
            $("#CId").val(obj.Content_ID);
            $("#title").val(obj.Title);
            $("#desc").val(obj.Description);
            $("#type option:selected").text(obj.TypeTbl.Name);
            $("#type option:selected").val(obj.Type_ID);
            $("#fromdate").val(obj.DateFrom);
            $("#todate").val(obj.DateTo);
        },
        failure: function (responce) {
            alert.responce("Record not Found");
        }
    });
}

Controller

public ActionResult EditUser(int userid)
{
    ContentTbl model = db.ContentTbls.Where(x => x.Content_ID == userid).SingleOrDefault();
    var editimg = model.Image_ID;
    TempData["eimageid"] = editimg;
    string value = string.Empty;
    value = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });
    JsonResult json = Json(value, JsonRequestBehavior.AllowGet);
    json.MaxJsonLength = int.MaxValue;    
    return json;      
}

View

<div id="imgPreview" class="thumbnail" style="display:none">
    <img class="img-responsive" id="targetImg" />
    <div class="caption">
        <span id="description"></span>
    </div>
    <a href="#" class="btn btn-danger" onclick="ClearPreview()"><iclass="glyphicon glyphicon-trash">Clear</i></a>
</div>
1
  • Convert image to byte array and the convert to base64 string Commented Mar 30, 2018 at 8:38

2 Answers 2

1

try this,

Controller:

string path = Path.Combine(Server.MapPath("~/Image"),Model.FileName);
using (var ms = new System.IO.MemoryStream(bytes))
{
     using (var img = Image.FromStream(ms, false, true)) 
     {
        img.Save(path);
     }
}

In view page give src of the image when you save the image.

Sign up to request clarification or add additional context in comments.

Comments

0

Convert raw image to byte array and then to ToBase64String

  Convert.ToBase64String(obj.ImageTbl.ImageByte.ToArray());

2 Comments

thnks bro for your comment but it does not work under ajax cal. i got this error " Uncaught ReferenceError: Convert is not defined "
You have to use it on server side, not in ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.