【问题标题】:Cannot display image from base64 string in javascript无法在 javascript 中显示来自 base64 字符串的图像
【发布时间】: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


【解决方案1】:

$.each() 返回其第一个参数的值,因此这将等同于编写:

+ post.JobPictures + 

你需要使用$.map(),回调函数需要返回一些东西,然后你可以用.join()将结果数组连接成一个字符串。

您不应该尝试将img 元素直接连接到字符串中。而是将 base64 字符串替换为 src 属性。

`JobImageContentBytes 中的数据已经被编码为base64,你不需要调用任何东西来编码它。

JobPictures 的开头有一个大写的J

$.map的回调函数的参数应该是elementindex

+ $.map(post.JobPictures, function (pictures) {
    if (pictures != null) {
        return `<div class="separate-pic">
                    <img class="posted-pic" src="data:image/jpg;base64,${pictures.JobImageContentBytes}" alt="" />
                </div>`;
     }
}).join("") + 

顺便说一句,使用模板文字的意义在于,您不需要结束字符串并使用+ 连接表达式,您可以使用${} 嵌入表达式。所以别写了

src="`+img+`"

改为写

src="${img}"

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-09-10
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多