refactor(intake): move download helpers
This commit is contained in:
parent
f5a3a6940a
commit
c4ffbcba0b
@ -331,6 +331,10 @@ import {
|
||||
mergeProductPatentRecordsWithProducts,
|
||||
} from "../productPatentStorage";
|
||||
import { syncProductPatentRecordFromProduct } from "./productPatentSyncHelpers";
|
||||
import {
|
||||
downloadBlob,
|
||||
resolveDownloadFileNameFromHeaders,
|
||||
} from "./downloadHelpers";
|
||||
import {
|
||||
previewSharedNonIntegratedDocumentImages,
|
||||
pushSharedNonIntegratedProductPricingToJushuitan,
|
||||
@ -15082,38 +15086,6 @@ export default function NonIntegratedDetailClient({
|
||||
return response.blob();
|
||||
}
|
||||
|
||||
function downloadBlob(blob: Blob, fileName: string) {
|
||||
const objectUrl = URL.createObjectURL(blob);
|
||||
const anchor = document.createElement("a");
|
||||
|
||||
anchor.href = objectUrl;
|
||||
anchor.download = fileName;
|
||||
document.body.appendChild(anchor);
|
||||
anchor.click();
|
||||
anchor.remove();
|
||||
URL.revokeObjectURL(objectUrl);
|
||||
}
|
||||
|
||||
function resolveDownloadFileNameFromHeaders(
|
||||
response: Response,
|
||||
fallbackFileName: string,
|
||||
) {
|
||||
const disposition = response.headers.get("content-disposition") ?? "";
|
||||
const encodedFileName = disposition.match(/filename\*=UTF-8''([^;]+)/i)?.[1];
|
||||
if (encodedFileName) {
|
||||
try {
|
||||
return decodeURIComponent(encodedFileName);
|
||||
} catch {
|
||||
return encodedFileName;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
disposition.match(/filename="?([^";]+)"?/i)?.[1]?.trim() ||
|
||||
fallbackFileName
|
||||
);
|
||||
}
|
||||
|
||||
async function readDownloadErrorMessage(response: Response) {
|
||||
const fallback = "合同下载失败,请稍后再试";
|
||||
try {
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
export function downloadBlob(blob: Blob, fileName: string) {
|
||||
const objectUrl = URL.createObjectURL(blob);
|
||||
const anchor = document.createElement("a");
|
||||
|
||||
anchor.href = objectUrl;
|
||||
anchor.download = fileName;
|
||||
document.body.appendChild(anchor);
|
||||
anchor.click();
|
||||
anchor.remove();
|
||||
URL.revokeObjectURL(objectUrl);
|
||||
}
|
||||
|
||||
export function resolveDownloadFileNameFromHeaders(
|
||||
response: Response,
|
||||
fallbackFileName: string,
|
||||
) {
|
||||
const disposition = response.headers.get("content-disposition") ?? "";
|
||||
const encodedFileName = disposition.match(/filename\*=UTF-8''([^;]+)/i)?.[1];
|
||||
if (encodedFileName) {
|
||||
try {
|
||||
return decodeURIComponent(encodedFileName);
|
||||
} catch {
|
||||
return encodedFileName;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
disposition.match(/filename="?([^";]+)"?/i)?.[1]?.trim() ||
|
||||
fallbackFileName
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user