当前位置:网站首页>Screenshot transferred to the background
Screenshot transferred to the background
2022-07-28 05:48:00 【I want to be a talker】
Send a screenshot of a part of the page to the back end
Detailed description of requirements
You need to put the histogram displayed on the page ( Or some preset area on other graphics or pages ) Take a screenshot and send it to the back end , The back end puts the pictures passed in from the front end into excel in , Automatic page download excel.
Realization
// Export pictures
handleExport() {
// Generate url
const canvas = document.createElement("canvas");// Create a canvas
let canvasBox = document.getElementById("export-region");// according to id Get what you want to intercept dom
const width = canvasBox.offsetWidth;
const height = canvasBox.offsetHeight;
canvas.width = width;
canvas.height = height;// Set the canvas to dom The width and height of
// When the generated page is vague , It can be amplified by a certain multiple , Through adjustment canvas The width and height of make it less clear
const context = canvas.getContext("2d");
context.scale(1, 1);
const options = {
backgroundColor: null,
canvas: canvas,
useCORS: true
};
html2canvas(canvasBox, options).then(canvas => {
let imgUrl = canvas.toDataURL("image/png");
// take base64 The file is converted to blob
let blob = dataURLtoBlob(imgUrl); // dataURL-> blob
let file = blobToFile(blob, " Screenshot .png"); // blob -> file
// Organizational parameters
let formData = new FormData();
// Add the binary file to formData in
formData.append("PictrueFile", file, " Screenshot .png");
exportPicture(formData).then(res => {
// above PictureFile The parameter name defined for the backend ,exportPicture To call the function of the back-end interface , It needs to be modified according to the actual situation
let resBlob = new Blob([res.data], {
type: "application/x-xlsx" });
let fileUrl = window.URL.createObjectURL(resBlob);
let link = document.createElement("a");
link.href = fileUrl;
link.setAttribute(
"download",
" Data and picture export _" + formatDate(+new Date(), "YYYY_MM_DD") + ".xlsx"
);//excel name ‘ Customize + current time ’
link.click();
});
});
},
// The two functions used dataURLtoBlob blobToFile
/** * take base64 Convert to blob */
function dataURLtoBlob(dataUrl){
var arr = dataUrl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {
type: mime
});
}
/** * take blob Convert to file */
export const blobToFile = (theBlob, fileName) => {
theBlob.lastModifiedDate = new Date();
theBlob.name = fileName;
return theBlob;
}
边栏推荐
猜你喜欢

Global event bus

Microsoft edge browser plug-in (2)

使用sourcetree推送仓库时 Failed to connect to www.google.com port 80: Timed out

树莓派串口配置

ArcGIS地图制作的注记、格网添加

Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later

微信公众号-授权登录

结果填空 凑算式(DFS*C语言)

ArcMap map map projection related operations

Sankey diagram drawing based on highcharts platform
随机推荐
js-promise实现逻辑
Centos7 install MySQL 5.7
Microsoft Edge浏览器插件(1)
微信公众号-授权登录
结果填空 李白打酒(心态炸了)
Sequence table OJ topic
Idea uses dev tool to realize hot deployment
结果填空 啤酒和饮料
设置滚动条
[idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea
Thinking on Architecture Design (SSO design)
冶金物理化学复习 --- 复杂反应的速率方程
es6新增数据类型--->Symbol、Map、Set
shell运行原理
Arcgis Engine安装的若干问题
基于XMind的E-R图制作
es6新增--->对象(Object)
冶金物理化学复习 --- 化学反应动力学基础
Event_looop-事件循环机制
C语言回顾(指针篇)