import html2canvas from "html2canvas"
import axios from 'axios'
const HtmlToFile = (refDom, id, imageName = '', scale = 1, imageType = "image/png", backgroundColor = '#fff') => {
if (imageName === '') {
let nowDate = new Date()
imageName = nowDate.getFullYear() + (nowDate.getMonth() + 1) + nowDate.getDate() + nowDate.getHours() + nowDate.getMinutes() + nowDate.getSeconds()
}
const canvas = document.createElement("canvas")
const width = parseInt(window.getComputedStyle(refDom).width)
const height = parseInt(window.getComputedStyle(refDom).height)
canvas.width = width * scalescale
canvas.height = height * scale
canvas.style.width = width + 'px'
canvas.style.height = height + 'px'
const ctx = canvas.getContext("2d");
ctx.scale(scale, scale);
const options = {
backgroundColor,
canvas,
useCORS: true
}
html2canvas(document.querySelector('#' + id), options).then((canvas) => {
let dataurl = canvas.toDataURL(imageType)
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);
}
let file = new File([u8arr], imageName + ".png", { type: mime });
let param = new FormData();
param.append('file', file, file.name);
axios.post('后端接口url', param,{
headers: {
'Content-Type': 'multipart/form-data',
}
}).then(res => {
})
}