SoFunction
Updated on 2025-03-08

How to pass the current window screenshot and convert the data base64 into png format to the server

Preface

Records come from demand

1. Take a screenshot of the current window and store the data

export default {
data() {
 return {
   image: '' //Storage data  }
}
 mounted() {
  setTimeout(() => {
      // Get the id of the position you need to intercept in the current dom      const dom = ("#cesium canvas");
      if (dom) {
        // Convert the selected dom element to a DataURL string in PNG format, print it out as base64 data         = ("image/png");
      }
   },500)
 }
}

2. Define the method to convert base64 to png

methods: {
     base64ImgtoFile(dataurl, filename = "file") {
      return new Promise((resolve, reject) => {
        const image = new Image();
         = dataurl;
         = () => {
          const canvas = ("canvas");
           = ;
           = ;
          const context = ("2d");
          (image, 0, 0);
          ((blob) => {
            const file = new File([blob], `${filename}.png`, {
              type: "image/png"
            });
            resolve(file);
          }, "image/png");
        };
         = (error) => reject(error);
      });
    },
    async convertBase64ToPNG(dataurl, fileName) {
      try {
        const file = await this.base64ImgtoFile(dataurl, fileName);
        // Use the converted file for subsequent operations        return file;
      } catch (error) {
        (error);
      }
    },
}

3. Complete code

<template>
  <el-button @click="handleSaveMap">submit</el-button>
</template>
<script>
export default {
  data() {
    return {
      image: "" //Storage data    };
  },
  mounted() {
    setTimeout(() => {
      // Get the id of the position you need to intercept in the current dom      const dom = ("#cesium canvas");
      if (dom) {
        // Convert the selected dom element to a DataURL string in PNG format, print it out as base64 data         = ("image/png");
      }
    }, 500);
  },
  methods: {
    base64ImgtoFile(dataurl, filename = "file") {
      return new Promise((resolve, reject) => {
        const image = new Image();
         = dataurl;
         = () => {
          const canvas = ("canvas");
           = ;
           = ;
          const context = ("2d");
          (image, 0, 0);
          ((blob) => {
            const file = new File([blob], `${filename}.png`, {
              type: "image/png"
            });
            resolve(file);
          }, "image/png");
        };
         = (error) => reject(error);
      });
    },
    async convertBase64ToPNG(dataurl, fileName) {
      try {
        const file = await this.base64ImgtoFile(dataurl, fileName);
        // Use the converted file for subsequent operations        return file;
      } catch (error) {
        (error);
      }
    },
    //Storage data to the server    async generateImages(image) {
      const file = new FileReader(); // Create an object      (image); // Read the file      const res = await (image); // Docking and uploading interface      const { object, msg, success } = ;
      if (success) {
         = ;
      } else this.$(msg);
    },
    // Save data    async handleSaveMap() {
      const loading = this.$loading({
        lock: true,
        text: "Save, please wait...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      // Intercept the current window data      await ();
      // Convert the current base64 image data to png      let image = await this.convertBase64ToPNG(, );
      //Storage to the server      if (image) {
        await (image);
        (, "");
      }
      ();
    }
  }
};
</script>

Summarize

This is the article about converting the current window screenshot and converting data base64 to png format to the server in Vue. This is all about the articles about converting data base64 to png format to transmit server content in Vue. Please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!