截图传入后台

将页面某部分截图传入后端

需求详细说明

需要将页面上展示的柱状图(或其他图形或页面上某个提前设定好的区域)进行截图并传给后端,后端将前端传入的图片放入excel中,页面自动下载excel。

实现

//导出图片
handleExport() {
    //生成url
    const canvas = document.createElement("canvas");//创建画布
    let canvasBox = document.getElementById("export-region");//根据id得到要截取的dom
    const width = canvasBox.offsetWidth;
    const height = canvasBox.offsetHeight;
    canvas.width = width;
    canvas.height = height;//将画布的设置为dom的宽高
    // 生成页面含糊时,能够放大肯定倍数,通过调整canvas的宽高使其清晰度减少
    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");
          //将base64文件转换为blob
          let blob = dataURLtoBlob(imgUrl); // dataURL-> blob
          let file = blobToFile(blob, "截图.png"); // blob -> file
          //组织参数
          let formData = new FormData();
          //将得到的二进制文件加入到formData中
          formData.append("PictrueFile", file, "截图.png");
          exportPicture(formData).then(res => {
          //上面的PictureFile为后端定义的参数名,exportPicture为调用后端接口的函数,需要根据实际情况进行修改
               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",
                     "数据和图片导出_" + formatDate(+new Date(), "YYYY_MM_DD") + ".xlsx"
                     );//excel命名‘自定义+当前时间’
                      link.click();
               });
         });
 },

//所用到的两个函数 dataURLtoBlob  blobToFile

/**
 * 将base64转换为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
  });

}

/**
 * 将blob转换为file
 */
export const blobToFile = (theBlob, fileName) => {
  theBlob.lastModifiedDate = new Date();
  theBlob.name = fileName;
  return theBlob;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 JSP 中将数据传入后台有多种方法,以下是其中两种常见的方法: 1. 通过表单提交: 在 JSP 页面中创建一个表单,表单中包含要传递给后台的参数,例如: ``` <form method="post" action="后台处理程序的URL"> <input type="text" name="param1" value="" /> <input type="text" name="param2" value="" /> <input type="submit" value="提交" /> </form> ``` 其中`method`属性指定提交方式为 POST,`action`属性指定后台处理程序的 URL,`name`属性指定参数名称,`value`属性指定参数值。当用户点击提交按钮时,表单数据将被提交到指定的后台处理程序中。 在后台处理程序中,可以通过 `request.getParameter()` 方法获取表单参数的值,例如: ``` String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); ``` 2. 通过 URL 传参: 在 JSP 页面中,将要传递给后台的参数拼接在 URL 中,例如: ``` <a href="后台处理程序的URL?param1=value1&param2=value2">提交</a> ``` 其中`href`属性指定链接的目标 URL,参数和参数值用 `param=value` 的形式拼接在 URL 末尾,多个参数之间用 `&` 分隔。当用户点击链接时,浏览器将会向后台处理程序发送一个 GET 请求,参数将会包含在请求的 URL 中。 在后台处理程序中,可以通过 `request.getParameter()` 方法获取 URL 参数的值,例如: ``` String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); ``` 需要注意的是,如果参数值中包含特殊字符(如空格、+ 等),需要进行 URL 编码后再传递,否则可能会导致参数值被解析错误。在 JSP 中可以使用 `URLEncoder.encode()` 方法进行 URL 编码,例如: ``` String param1 = URLEncoder.encode("value with spaces", "UTF-8"); String param2 = URLEncoder.encode("value+with+plus", "UTF-8"); String url = "后台处理程序的URL?param1=" + param1 + "&param2=" + param2; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值