如何将获取的图像URL附加到IMG SRC

发布于 2025-01-24 12:01:39 字数 1802 浏览 3 评论 0原文

我试图将所取的URL从铸造的NFT JSONS元数据附加到a< img src,以便然后在DAPP内看到图片。我已经从收据中获取了代币,然后将tokenid与ipfs链接使用来获取元数据,然后返回图像URL,所有这些都可以正常工作,我刚刚隐藏了一些带有个人信息的代码。

      .then(async(receipt) => {
        console.log(receipt);
        setMinted(true)
       const tokenIds = {ipfslink}.json

          const tokenMetadataResponse = await fetch(`/config/json/${tokenIds}.json`, {
            headers: {
              "Content-Type": "application/json",
              Accept: "application/json",
            },
          });
          const tokenMetadata = await tokenMetadataResponse.json();

          const image = tokenMetadata.image;
          console.log(image)

          const nftHolder = document.getElementById("nft_template").content.cloneNode(true)
          nftHolder.querySelector("img").src = image.toString();
          nftHolder.querySelector("img").alt = tokenMetadata.description
    
          document.getElementById("nfts").append(nftHolder)

当我主持链接时,我会得到正确的映像URL,但是当我尝试附加时,我会得到此错误图像描述在这里(我无法嵌入我的道歉),所有人都帮助您感谢。

这是IMG元素所在的地方,我有一个视频,该视频在收到交易时播放,这表明我打算使用图像播放的动画成功的薄荷。 因此,当收据发生时,它将铸造状态转变为True,您可以在上面的代码中看到

    {minted ? <s.Screen id="nft">
      <MintingVideo src="/config/mint.mp4" autoPlay={true} ></MintingVideo> 
      <template id="nft_template">
    <section>
      <h1></h1>

      <a href="" target="_blank">
        <img src={null} alt=""></img>
      </a>
    </section>
  </template>
    </s.Screen>
    : null}

I am trying to append the fetched url from a minted NFT jsons metadata to a <img src so that the picture can then be seen inside the dapp. I have fetched the tokenId from the receipt and then used the tokenId with the ipfs link to grab the metadata and then return the image URL which all works fine I have just hidden some code with the personal information.

      .then(async(receipt) => {
        console.log(receipt);
        setMinted(true)
       const tokenIds = {ipfslink}.json

          const tokenMetadataResponse = await fetch(`/config/json/${tokenIds}.json`, {
            headers: {
              "Content-Type": "application/json",
              Accept: "application/json",
            },
          });
          const tokenMetadata = await tokenMetadataResponse.json();

          const image = tokenMetadata.image;
          console.log(image)

          const nftHolder = document.getElementById("nft_template").content.cloneNode(true)
          nftHolder.querySelector("img").src = image.toString();
          nftHolder.querySelector("img").alt = tokenMetadata.description
    
          document.getElementById("nfts").append(nftHolder)

When I console.log the link I get the correct Image Url but when I try to append I get this error enter image description here (I can not embed yet my apologies) All help appreciated thank you.

Here is where the img element lies, I have a video that plays upon the receipt of the transaction that indicates a successful mint for the animation that I intend to play with the image.
So when the receipt occurs it turns the minted state to true which you can see in the above code

    {minted ? <s.Screen id="nft">
      <MintingVideo src="/config/mint.mp4" autoPlay={true} ></MintingVideo> 
      <template id="nft_template">
    <section>
      <h1></h1>

      <a href="" target="_blank">
        <img src={null} alt=""></img>
      </a>
    </section>
  </template>
    </s.Screen>
    : null}

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

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

发布评论

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

评论(1

北风几吹夏 2025-01-31 12:01:39

我已经通过给图像作为占位符的来源来解决这一问题,这只是白色背景。

    <s.Screen> //Screen holding all components displayed whether minted is true or not
    {minted ? <s.Screen id="nft">
      <MintingVideo src="/config/mint.mp4" autoPlay={true} ></MintingVideo> 

        <NftBox id="nftBox"src="/config/images/plah.png"></NftBox>

    </s.Screen>
    : null}

然后,这是我针对元素的地方,然后将源更改为提供的元素

          const tokenMetadataResponse = await fetch(`/config/json/${tokenIds}.json`, {
            headers: {
              "Content-Type": "application/json",
              Accept: "application/json",
            },
          });
          const tokenMetadata = await tokenMetadataResponse.json();

          let image = tokenMetadata.image
           if(image.startsWith("ipfs://")) {
              image = `https://ipfs.io/ipfs/${tokenMetadata.image.split("ipfs://")[1]} `
          };

          console.log(image)

           const nftHolder = document.getElementById("nft").content
           const dNftBox = document.getElementById("nftBox");
           dNftBox.src = image;

I have solved this I believe by giving the image a placeholder source that is just a white background.

    <s.Screen> //Screen holding all components displayed whether minted is true or not
    {minted ? <s.Screen id="nft">
      <MintingVideo src="/config/mint.mp4" autoPlay={true} ></MintingVideo> 

        <NftBox id="nftBox"src="/config/images/plah.png"></NftBox>

    </s.Screen>
    : null}

And then here is where I target the element and change the source to the one provided

          const tokenMetadataResponse = await fetch(`/config/json/${tokenIds}.json`, {
            headers: {
              "Content-Type": "application/json",
              Accept: "application/json",
            },
          });
          const tokenMetadata = await tokenMetadataResponse.json();

          let image = tokenMetadata.image
           if(image.startsWith("ipfs://")) {
              image = `https://ipfs.io/ipfs/${tokenMetadata.image.split("ipfs://")[1]} `
          };

          console.log(image)

           const nftHolder = document.getElementById("nft").content
           const dNftBox = document.getElementById("nftBox");
           dNftBox.src = image;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文