【发布时间】:2019-05-06 19:46:40
【问题描述】:
我正在尝试使用 javascript 将已格式化为 base64 的原始图像显示为 img 元素。我已经有了base64,但不能让它显示为原始图像,代码有什么问题?
这是代码。
$.each(post.JobPictures, function (i, pictures) {
if (pictures != null) {
var decodedString = atob(pictures.JobImageContentBytes)
var img = new Image();
img.src = ("data:image/jpg;base64,"+ decodedString);
`<div class="separate-pic">
<img class="posted-pic" src="`+img+`" alt="" />
</div>`
}
})
现在正在使用 MAP 进行更新,但它根本无法进入
$.ajax({
url: '@Url.Action("SearchByCategory", "AllJobs")',
type: 'GET',
contentType: 'JSON',
data: { category: categoryId },
success: function (posts) {
$(".job-container").html("");
//$(".job-container").load(" .job-container");
$.each(posts.FindJobs, function (i, post) {
var newdate = new Date(post.PostedDate + 'Z');
var day = newdate.getDate();
$(".job-container").append(`
<li class="separate-job" id="All-Jobs-Id" value="` + post.jobId + `">
<div class="content-li-All-Jobs">
<h2 class="content-li-All-headline" id="headline-for-Update">`+ post.Headline + `</h2>
<a class="btn btn-success bid-for-job" value="`+ post.jobId + `" href="#">Bid now</a>
<div class="user">
<blockquote class="blockquote">
<p class="mb-0">
<div class="about-job">`+ post.About + `</div>
</p>
<div class="blockquote-footer">
<cite>-`+ post.Username + `</cite>
</div>
</blockquote>
</div>
<div class="pictures-li">
`+ $.map(post.jobPictures, function (i, pictures) {
if (pictures != null) {
var decodedString = atob(pictures.JobImageContentBytes)
return `<div class="separate-pic">
<img class="posted-pic" src="data:image/jpg;base64,${decodedString}" alt="" />
</div>`;
}
}).join("") + `
</div>
<div class="job-date-li">
Posted `+ newdate.getDate() + ` ` + newdate.getMonth() + ` ` + newdate.getFullYear() +
`
</div>
<div class="-job-town">Area | <span class="city">`+ post.JobCity+`</span></div>
</div>
</li>
`)
});
}
});
这是我对控制器进行的整个 ajax 调用,以获取特定类别中的所有作业并创建这些 div 并将相关数据分配给它们
这是显示对象/数组不为空的图像
【问题讨论】:
-
@Taplar 对不起,我的错,我在 .append 方法中使用了以下代码 sn-p。现在更清楚了?
-
所以,基本上,我有一个包含一些字节的数组,我循环遍历这些项目以显示 img 并创建 div+ img 元素。我还应该怎么做?我真的很困惑
-
@Taplar 我不想用它做任何事情 - 我想做的就是在 div 中显示 img ,并根据数组的长度生成带有这些图像的 div .
-
我将更新帖子以查看整个附加内容以及其中的内容...
-
contentType: 'JSON',错了,应该是dataType: 'json'
标签: javascript jquery asp.net-mvc image base64