From d2e867966539004b6b5a73ae3566a659ac6f8b6d Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期四, 30 十月 2025 11:55:34 +0800
Subject: [PATCH] 检验任务数据分类查询问题修复
---
src/utils/file.js | 92 ++++++++++++++++++++++++++++++++++------------
1 files changed, 68 insertions(+), 24 deletions(-)
diff --git a/src/utils/file.js b/src/utils/file.js
index c3a3e7b..ac6ad7b 100644
--- a/src/utils/file.js
+++ b/src/utils/file.js
@@ -1,48 +1,55 @@
-import { convertToHtml } from 'mammoth';
-import Vue from 'vue'
+import { convertToHtml } from "mammoth";
+import pako from "pako";
+import Vue from "vue";
export default {
async convertFileToHtml(url) {
var xhr = new XMLHttpRequest();
- xhr.open('GET', Vue.prototype.javaApi+url, true);//鑾峰彇鏂囦欢娴佺殑鎺ュ彛
+ xhr.open("GET", Vue.prototype.javaApi + url, true); //鑾峰彇鏂囦欢娴佺殑鎺ュ彛
xhr.send();
- xhr.responseType = "blob";//涓嶈兘婕�
+ xhr.responseType = "blob"; //涓嶈兘婕�
let xhrPromise = new Promise((resolve, reject) => {
xhr.onload = async function () {
if (this.status === 200) {
// 杩斿洖鐨勬枃浠舵祦锛岃浆鎹㈡垚blob瀵硅薄
- var blob = new Blob([this.response],{ type:'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
+ var blob = new Blob([this.response], {
+ type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ });
// 浣跨敤mammoth灏哤ord杞崲涓篐TML
let reader = new FileReader();
reader.readAsArrayBuffer(blob);
let htmlContentPromise = new Promise((resolve, reject) => {
reader.onload = async function () {
var arrayBuffer = xhr.response; //arrayBuffer
- const result = await convertToHtml({ arrayBuffer: arrayBuffer })
- let html = result.value.replace(/飪�/g, '')
- .replace('<h1>', '<h1 style="text-align: center;">')
- .replace(/<table>/g, '<table style="border-collapse: collapse;border: 1px solid #000;">')
- .replace(/<tr>/g, '<tr style="height: 30px;">')
- .replace(/<td>/g, '<td style="border: 1px solid #000;">')
- .replace(/<p>/g, '<p style="text-indent: 2em;">')
- .replace(/<a [^>]*>/g, "")
- .replace(/<\/a>/g, "")
+ const result = await convertToHtml({ arrayBuffer: arrayBuffer });
+ let html = result.value
+ .replace(/飪�/g, "")
+ .replace("<h1>", '<h1 style="text-align: center;">')
+ .replace(
+ /<table>/g,
+ '<table style="border-collapse: collapse;border: 1px solid #000;">'
+ )
+ .replace(/<tr>/g, '<tr style="height: 30px;">')
+ .replace(/<td>/g, '<td style="border: 1px solid #000;">')
+ .replace(/<p>/g, '<p style="text-indent: 2em;">')
+ .replace(/<a [^>]*>/g, "")
+ .replace(/<\/a>/g, "");
// .replace(/em/g, "cm");
- resolve(html)
+ resolve(html);
};
- })
- resolve(await htmlContentPromise)
+ });
+ resolve(await htmlContentPromise);
}
-
- }
- })
- return await xhrPromise
+ };
+ });
+ return await xhrPromise;
},
- downloadIamge(imgsrc, name) {//涓嬭浇鍥剧墖鍦板潃鍜屽浘鐗囧悕
+ downloadIamge(imgsrc, name) {
+ //涓嬭浇鍥剧墖鍦板潃鍜屽浘鐗囧悕
var image = new Image();
// 瑙e喅璺ㄥ煙 Canvas 姹℃煋闂
image.setAttribute("crossOrigin", "anonymous");
- image.onload = function() {
+ image.onload = function () {
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
@@ -57,5 +64,42 @@
a.dispatchEvent(event); // 瑙﹀彂a鐨勫崟鍑讳簨浠�
};
image.src = imgsrc;
- }
+ },
+ // 鍘嬬缉blob
+ compressBlob(blob) {
+ const reader = new FileReader();
+ reader.readAsArrayBuffer(blob);
+ return new Promise((resolve) => {
+ reader.onload = () => {
+ const arrayBuffer = reader.result;
+ const uint8Array = new Uint8Array(arrayBuffer);
+ const compressedData = pako.deflate(uint8Array);
+ const compressedBlob = new Blob([compressedData], { type: blob.type });
+ resolve(compressedBlob);
+ };
+ });
+ },
};
+
+export function transformExcel(response, tempName) {
+ const relType = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel;charset=UTF-8']
+ let type = response.type
+ if (relType.includes(type)) {
+ const blob = new Blob([response], {type: 'application/vnd.ms-excel'})
+ let temp = tempName
+ if(response.headers){
+ const disposition = response.headers["Content-Disposition"]
+ temp = disposition.substring(disposition.lastIndexOf('=') + 1)
+ }
+ let filename = decodeURI(temp)
+ // 鍒涘缓涓�涓秴閾炬帴锛屽皢鏂囦欢娴佽祴杩涘幓锛岀劧鍚庡疄鐜拌繖涓秴閾炬帴鐨勫崟鍑讳簨浠�
+ const elink = document.createElement('a')
+ elink.download = filename
+ elink.style.display = 'none'
+ elink.href = URL.createObjectURL(blob)
+ document.body.appendChild(elink)
+ elink.click()
+ URL.revokeObjectURL(elink.href) // 閲婃斁URL 瀵硅薄
+ document.body.removeChild(elink)
+ }
+}
--
Gitblit v1.9.3