我的堆栈是Oracle DB、Node.js和Reaction Web.我在Oracle DB中有一个存储为Blob的图像.我想在页面中查看此图像,但失败了.我将包含图像的对象置于状态,但无法查看它.这是包含图像的对象:

{
  USER_ID:5,
  USER_IMAGE:{type: 'Buffer', data: Array(153120)
}

这是组件:

  const { state: editUserId } = useLocation();
  const userId = useUserId();
  const userInitState = {
    userId: null,
    userImage: null,
  };
  const [user, setUser] = useState({ ...userInitState });
  const [profileImage, setProfileImage] = useState();

  const {
    // data: posts = [],
    data: userProfile = [],
    isUsersLoading,
    isUsersSuccess,
    isUsersFetching,
    isUsersError,
    Userserror,
  } = useGetUserAccountQuery(editUserId);

  useEffect(() => {
    if (userProfile?.data) {
      const fetchedUser = userProfile?.data;
      setUser({
        userId: fetchedUser.USER_ID,
        userImage: fetchedUser.USER_IMAGE,
      });
      const file = new Blob([fetchedUser?.USER_IMAGE], {
        type: "image/jpeg",
      });
      const fileUrl = URL.createObjectURL(file);
      setProfileImage(fileUrl);
    }
  }, [editUserId, userProfile]);

  console.log("UserProfile2 profileImage: ", profileImage); //blob:http://localhost:3000/9e880575-e302-4bc7-bea2-b2ab8f3923cd

  return (
    <div>
      <img
        src={profileImage}
        // src={URL.createObjectURL(profileImage)}
        alt="img"
        width={200}
        height={200}
      />
    </div>
  );
};

推荐答案

try 先将图像JSON数据转换为类型化数组(data: Array(153120)属性):

const file = new Blob([new Uint8Array(fetchedUser.USER_IMAGE.data)], {
  type: "image/jpeg",
});

Node.js相关问答推荐

使用HTTPS从NodeJS 17.9.1升级到18.0.0后,SignalR连接失败

如何使用NodeJS处理来自请求的单个或多个文件?

Firebase-admin筛选器.或只考虑第一个WHERE子句

如何在mongodb集合中设置数据限制?

函数声明中的异步在没有等待的函数中做什么?

如何模拟 mysql2 `getConnection`

在 TypeScript 中正确键入 MongoDB find 方法

当其中一个端点不工作时,如何使用 axios.all() 调用多个 API?

为什么我的 Cypress Post 请求的请求正文是空的?

(Mongoose) 删除 TTL 字段失败

fastify:流过早关闭

Discord.js V14 interaction.editReply 给出了一个无效的 webhook 令牌错误

多个 Axios 请求合并

当我们有esnext时,为什么我们需要nodenext typescript 编译器选项?

在 Javascript 逻辑中访问 EJS 变量

为什么模块级返回语句在 Node.js 中起作用?

从 zip 文件在 AWS 中创建 lambda 函数

Node.js:如何附加到正在运行的进程并使用控制台调试服务器?

- configuration.output.path:提供的值public不是绝对路径!使用 Webpack

npm install - javascript堆内存不足