简体   繁体   English

如何在 React 中显示从 Azure Blob 存储下载的图像

[英]How to display image downloaded from Azure Blob Storage in React

First of all, Ive checked this question and it's not a duplicate How to display an image saved as blob in React首先,我检查了这个问题,它不是重复的如何在 React 中显示保存为 blob 的图像

I'm not sure what they're doing, but our code is entirely different.我不确定他们在做什么,但我们的代码完全不同。

I have been following Azure's documentation on downloading files from blob storage via React: https://docs.microsoft.com/en-us/javascript/api/overview/azure/storage-blob-readme?view=azure-node-latest#download-a-blob-and-convert-it-to-a-string-browsers我一直在关注 Azure 关于通过 React 从 blob 存储下载文件的文档: https ://docs.microsoft.com/en-us/javascript/api/overview/azure/storage-blob-readme?view=azure-node-latest #download-a-blob-and-convert-it-to-a-string-browsers

Here is my component did mount, where I'm downloading the images from my blob storage container.这是我安装的组件,我正在从我的 blob 存储容器下载图像。

    async function blobToString(blob) { // this is copy-pasted from Azure who provided this as a helper function
        const fileReader = new FileReader();
        return new Promise((resolve, reject) => {
            fileReader.onloadend = (ev) => {
                resolve(ev.target.result);
            };
            fileReader.onerror = reject;
            fileReader.readAsText(blob);
        });
    }

    useEffect(() => {
        async function fetchData() {
            if (!mounted) {
                console.log(images)
                setImages(images.map(async (image) => {
                    // console.log(image)
                    const blobClient = containerClient.getBlobClient(image.src);
                    let downloadBlockBlobResponse = await blobClient.download()
                    let downloaded = await blobToString(await downloadBlockBlobResponse.blobBody)
                    // console.log(downloaded)
                    image.src = URL.createObjectURL(downloadBlockBlobResponse);
                    return image;
                }));
                console.log(images)
                setMounted(true);
            }
        }
        fetchData();
    }, []);

    return (
        <>
            {mounted ? <>
                <img src={images[0].src} />
            </> : null}
        </>
    )

When I console.log downloaded , which is the string version of the image, this is what I see:当我console.log downloaded时,这是图像的字符串版本,这就是我看到的:

在此处输入图像描述

Chrome says it's a 1.1 mb string, so I'm sure it's the entire image. Chrome 说它是一个 1.1 mb 的字符串,所以我确定它是整个图像。

I know that this is not a valid image src because the screen is entirely blank and the image being pulled from blob storage is not a white picture.我知道这不是有效的图像src ,因为屏幕完全是空白的,并且从 blob 存储中提取的图像不是白色图片。 (There is no doubt that mounted is being set correctly, I'm actually running this image through a custom component which is also registering that the image source is invalid, and you'll just have to believe that I'm passing the source correctly to this component) (毫无疑问, mounted设置正确,我实际上是通过一个自定义组件运行这个图像,该组件也注册图像源无效,你只需要相信我正确地传递了源到这个组件)

在此处输入图像描述

Any idea how I can create a valid image source from this convoluted string Azure has produced?知道如何从 Azure 产生的这个复杂的字符串创建一个有效的图像源吗?

edit:编辑:

在此处输入图像描述 在此处输入图像描述

unfortunately, whatever this blob is, is still not registering as a valid image source.不幸的是,无论这个 blob 是什么,仍然没有注册为有效的图像源。 my code is a little bit different because I was getting this error ( Failed to execute 'createObjectURL' on 'URL': ) so I followed the instructions there.我的代码有点不同,因为我收到了这个错误( 无法在 'URL' 上执行 'createObjectURL': )所以我按照那里的说明进行操作。

This also does not work:这也不起作用: 在此处输入图像描述

The Azure SDK documentation example just show how you would consume the downloaded Blob as a string using blobToString() (assuming the blob is text). Azure SDK 文档示例仅展示了如何使用blobToString()将下载的 Blob 作为字符串使用(假设 Blob 是文本)。 In your case you don't need do convert it to string.在您的情况下,您不需要将其转换为字符串。 Could you please try你能试试吗

const downloaded = await downloadBlockBlobResponse.blobBody;
image.src = URL.createObjectURL(downloaded);

Edited: Not sure what you are missing.编辑:不知道你错过了什么。 but I just tried, and this simple page shows the downloaded image correctly.但我刚试过,这个简单的页面正确显示了下载的图像。

export default function Page(): JSX.Element {
  const [imgSrc, setImgSrc] = useState<string>("");
  useEffect(() => {
    async function fetchImage() {
      if (!downloaded) {
        if (imageUrl) {
          client = new BlockBlobClient(imageUrl);
          const response = await client.download();
          const blob = await response.blobBody;
          if (blob) {
            setImgSrc(URL.createObjectURL(blob));
            console.log("##### ", imgSrc);
          }
        }
        downloaded = true;
      }
    }
    fetchImage();
  }, [imgSrc]);

  return (
    <>
      <div className="jumbotron">
        <h1>Storage Blob Download Sample!</h1>
        <div className="alert alert-light">
          <h4 className="alert-heading">Image</h4>
          <div className="container">
            <img src={imgSrc} />
          </div>
        </div>
      </div>
    </>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM