Display Byte type images obtained from the PostgreSQL database on the Vue front-end

  1. Backend processing:

    In the backend (Node.js, Express, or other framework), ensure that you can obtain byte-type image data from the PostgreSQL database. This may involve using an appropriate ORM (Object Relational Mapping) tool or manually querying the database.

    For example, if you use Node.js and Express, you may get image data in a way similar to the following:

    app.get('/api/getImage', (req, res) => {
          
          
      // 查询数据库获取字节数据
      // 这里假设你使用了某个 ORM,具体情况可能有所不同
      const imageData = // 获取从数据库查询得到的字节数据;
    
      // 将字节数据发送给前端
      res.send(imageData);
    });
    
  2. Front-end processing:

    In the Vue front-end, you need to use appropriate methods to convert byte data into usable image formats. Typically, you can use Blob objects to handle byte data. In Vue components, this can be achieved in the following ways:

    <template>
      <div>
        <img :src="imageUrl" alt="Image" />
      </div>
    </template>
    
    <script>
    export default {
          
          
      data() {
          
          
        return {
          
          
          imageUrl: '', // 存储图片的URL
        };
      },
      mounted() {
          
          
        this.getImage();
      },
      methods: {
          
          
        async getImage() {
          
          
          // 发送请求获取字节数据
          const response = await fetch('/api/getImage');
          const imageData = await response.blob();
    
          // 将Blob对象转换为DataURL
          const imageUrl = URL.createObjectURL(imageData);
    
          // 更新Vue组件的imageUrl
          this.imageUrl = imageUrl;
        },
      },
    };
    </script>
    

    In this code, we fetchget the byte data by using, then convert the Blob object to a DataURL, and finally set it as the imageUrl of the Vue component.

Guess you like

Origin blog.csdn.net/weixin_49015143/article/details/135117539