我想在单击编辑按钮后在预览中显示图片,但它在列表中以二进制形式获取图片,然后通过Json结果将其传递给ajax调用,之后它不会在ajax调用下转换。任何人都可以帮我解决这个问题。谢谢
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");
}
});
}
控制器
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;
}
查看
<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>
答案 0 :(得分:1)
试试这个,
控制器:
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);
}
}
在视图页面中,在保存图像时给出图像的src。
答案 1 :(得分:0)
将原始图像转换为字节数组,然后转换为ToBase64String
Convert.ToBase64String(obj.ImageTbl.ImageByte.ToArray());