【问题标题】:Why is my base64 string only display part of my picture?为什么我的 base64 字符串只显示我图片的一部分?
【发布时间】:2017-09-15 02:26:46
【问题描述】:

在上一个问题here 上,我收到了社区的帮助,将生成的 base64 字符串设置为 src 属性,但是,图像缺少图像的底部。请看下图。

下面是我用来设置图像内容的代码。如何让图像的其余部分显示?

import React from 'react';
import axios from 'axios';
import * as settings from './settings';

export default class Post extends React.Component {
    constructor(){
        super();
        this.state = {
            profileImage:'img/loading.gif'
        };
    }
    componentDidMount(){
        if(this.props.post.PostImageId != "00000000-0000-0000-0000-000000000000"){
            // get post image data
            _rvw.getImage(this.props.post.PostImageId)
            .then((res)=>{
                this.setState({
                    postImage: "data:" + res.headers["content-type"] + ";base64," + res.data
                });
            });
        }
        else //no image
        {
            this.setState({
                postImage: "img/no-image.png"
            });
        }
    }
    getImage(id){
        let config = {
            headers:{
                'Content-Type': 'application/json',
                'Accept':'application/json',
                'Authorization':'Bearer ' + localStorage.token
            }
        };
        return axios.get(settings.baseUrl()+'/mediacontent/get/'+id,config);
    }
    render(){
        return (<div className="image">
                <img ref="postMedia" src={this.state.postImage} />
            </div>);
    }
}

完整的组件可以在here找到。

【问题讨论】:

  • 先检查收到的数据是否完整。
  • 无关紧要,但仍然:顺便说一句,this.props.post.ProfileImageId != null 检查可能是多余的。
  • 我仔细检查了尺寸,它是正确的。 azure 上的 blob 为 289.9kb,我从 http 响应中收到 290kb。
  • 等等,二进制数据的blob是289k?还是 289kb 的 base64?

标签: javascript c# azure reactjs azure-blob-storage


【解决方案1】:

&lt;img ref="postMedia" src={this.state.postImage} /&gt;

根据您的帖子,src 的值应该是 this.state.profileImage 而不是 this.state.postImage。还请检查图像的其他部分是否被其他 UI 元素覆盖。您可以使用浏览器提供的 DOM Explorer 窗口来检查 html 元素和应用于这些 html 元素的样式表。

【讨论】:

  • 您好 Amor,我继续进行了一些更正。我试图从完整组件中提取代码以仅显示与我遇到的问题有关的内容,但现在我继续并添加了指向完整组件代码的链接。我希望这会增加更多的上下文。
  • 我建议你从 C# 端和 JS 端都打印出 base64 字符串。检查它们是否不同。然后,您可以创建一个带有一个图像标签的简单 html 文件,并使用 base64 字符串设置 src 属性。它将帮助您定位错误位置。
猜你喜欢
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 2019-02-14
  • 1970-01-01
相关资源
最近更新 更多