当前位置:网站首页>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;
}
边栏推荐
- 环形链表问题
- 记录某某小卢的第一篇文章
- 顺序表oj之合并两个有序数组
- Interface idempotency problem
- 树莓派蓝牙调试过程
- Online word cloud generation (taking WordArt as an example)
- Annotation and grid addition of ArcGIS map making
- [idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea
- 结果填空 李白打酒(心态炸了)
- CAD-GIS数据转换
猜你喜欢

shell运行原理

Global event bus

Review of metallurgical physical chemistry -- Fundamentals of metallurgical reaction kinetics and characteristics of multiphase reaction kinetics

Leetcode 随即链表的深拷贝

Review of metallurgical physical chemistry --- electrodeposition and reduction process of metals

树莓派WIFI一键连接配置

Sequence table OJ topic

冶金物理化学复习 --- 金属的电沉积,还原过程

蓝桥代码 翻硬币(我这样写也通过了,官网测试是不是有问题)

Online word cloud generation (taking WordArt as an example)
随机推荐
标准C语言学习总结8
HDU-1284:钱币兑换问题 推理+动态规划(dp)
Arcgis Engine安装的若干问题
[idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea
蓝桥代码 错误票据
pytorch安装----CPU版的
ArcGIS Engine开发资源
结果填空 奖券数目(dfs * 数学公式)
GD32F407 移植FreeRTOS+Lwip
标准C语言学习总结6
冶金物理化学复习 --- 化学反应动力学基础
ArcGIS Engine Development Resources
Child parent thread interaction
C语言回顾(修饰词篇)
Database interview
new,let,var,const以及暂时性死区,xml和json的区别
Problem set in the project
标准C语言总结2
排序之插入排序
TopK问题