使用本地路径在ReactJ中显示图像

发布于 2025-01-22 09:47:06 字数 3297 浏览 0 评论 0原文

“使用本地路径在ReactJ中显示图像” 我只能提供一个URL来获取图像,无论是在线或使用localhost托管本地图像。如何使用本地路径目标而不是URL获取图像。

if(mydata [“ status”] ==“ active”){ FinalTable +=“状态:”; } 这是我必须提供本地路径以获取图像的部分。 请帮忙 谢谢。

    const designHtml = (evt, mySource) => {
    document.getElementById('popup-content').style.display = 'none';        
    let viewResolution = /** @type {number} */ (myView.getResolution());
    let url = mySource.getFeatureInfoUrl(
        evt.coordinate,
        viewResolution,
        'EPSG:3857',
        { 'INFO_FORMAT': 'application/json', }
    );

  
    if (url) {
        fetch(url)
            .then((response) => response.text())
            .then((json) => {
                var myJSON = json;
                const myObj = JSON.parse(myJSON);
                // console.log(myObj);
                if (myObj.features.length != 0) {
                    document.getElementById('popup-content').style.display = 'block';
                    // var1.style.display ='block'
                    var myData = myObj.features[0].properties;


                    myOverlay.setPosition(evt.coordinate);
                    console.log("myData", myData);
                    var finalTable = "<tr class='row'>Table Row";
                    finalTable += "<table id='myTable' style='border: 1px solid red'>" + "<tbody>" +
                        
                        "<tr><td> Cell Name: </td>" + "<td>" + myData["cell_name"] + "</td> </tr>" +
                        "<tr><td> Muncipal: </td>" + "<td>" + myData["municipio"] + "</td> </tr>" +
                        "<tr><td> Node Name: </td>" + "<td>" + myData["node_name"] + "</td> </tr>" +
                        "<tr><td> Provincia: </td>" + "<td>" + myData["provincia"] + "</td> </tr>" +
                        "<tr><td> Status: </td>" + "<td>" + myData["status"] + "</td> </tr>" 
                       

                   

                    if (myData["status"] == "Active") {
                        
                          finalTable += "<tr><td> Status:</td><td>  <img src ='http://localhost/download.jpg' style ='height: 50px; width:50px'/> </td> </tr>";
                       
                    }
                    else {
                        console.log(myData["status"]);
                        finalTable += "<tr><td>  <img src ='https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg' style ='height: 50px; width:50px'/> </td></tr>";
                        //finalTable += "<tr><td> <img src=" + 'data:image/png;base64,' + inactiveImageURL + " /></td></tr>";
                    }
                    finalTable += "</tbody> </table> </tr>";
                    document.getElementById('popup-content').innerHTML += finalTable;
                    myOverlay.setPosition(evt.coordinate);

                }
                else {
                    document.getElementById('popup-content').style.display = 'none';

                }

            });

    }

}

"display image in reactjs using local path"
I can only give a url to fetch the image,either from online or hosting the local image using localhost.How to fetch the image using local path destination instead of the urls.

if (myData["status"] == "Active") {
finalTable += " Status: ";
}
this is the part where i have to give the local path to fetch the image.
Please help,
Thank you.

    const designHtml = (evt, mySource) => {
    document.getElementById('popup-content').style.display = 'none';        
    let viewResolution = /** @type {number} */ (myView.getResolution());
    let url = mySource.getFeatureInfoUrl(
        evt.coordinate,
        viewResolution,
        'EPSG:3857',
        { 'INFO_FORMAT': 'application/json', }
    );

  
    if (url) {
        fetch(url)
            .then((response) => response.text())
            .then((json) => {
                var myJSON = json;
                const myObj = JSON.parse(myJSON);
                // console.log(myObj);
                if (myObj.features.length != 0) {
                    document.getElementById('popup-content').style.display = 'block';
                    // var1.style.display ='block'
                    var myData = myObj.features[0].properties;


                    myOverlay.setPosition(evt.coordinate);
                    console.log("myData", myData);
                    var finalTable = "<tr class='row'>Table Row";
                    finalTable += "<table id='myTable' style='border: 1px solid red'>" + "<tbody>" +
                        
                        "<tr><td> Cell Name: </td>" + "<td>" + myData["cell_name"] + "</td> </tr>" +
                        "<tr><td> Muncipal: </td>" + "<td>" + myData["municipio"] + "</td> </tr>" +
                        "<tr><td> Node Name: </td>" + "<td>" + myData["node_name"] + "</td> </tr>" +
                        "<tr><td> Provincia: </td>" + "<td>" + myData["provincia"] + "</td> </tr>" +
                        "<tr><td> Status: </td>" + "<td>" + myData["status"] + "</td> </tr>" 
                       

                   

                    if (myData["status"] == "Active") {
                        
                          finalTable += "<tr><td> Status:</td><td>  <img src ='http://localhost/download.jpg' style ='height: 50px; width:50px'/> </td> </tr>";
                       
                    }
                    else {
                        console.log(myData["status"]);
                        finalTable += "<tr><td>  <img src ='https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg' style ='height: 50px; width:50px'/> </td></tr>";
                        //finalTable += "<tr><td> <img src=" + 'data:image/png;base64,' + inactiveImageURL + " /></td></tr>";
                    }
                    finalTable += "</tbody> </table> </tr>";
                    document.getElementById('popup-content').innerHTML += finalTable;
                    myOverlay.setPosition(evt.coordinate);

                }
                else {
                    document.getElementById('popup-content').style.display = 'none';

                }

            });

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2025-01-29 09:47:06

在ReactJ中,如果要加载本地图像,则必须使用require()如下

<img src={require('../assets/images/image.png')} style ='height: 50px; width:50px' />

In reactJS if you want to load local image then you have to use require() like below

<img src={require('../assets/images/image.png')} style ='height: 50px; width:50px' />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文