Skip to content

Commit

Permalink
chore: 统一文件下载逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Feb 25, 2025
1 parent 346a391 commit 86809d2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 35 deletions.
27 changes: 2 additions & 25 deletions packages/amis-core/src/utils/attachmentAdpator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {ApiObject} from '../types';
import {saveAs} from 'file-saver';

export function attachmentAdpator(
response: any,
Expand Down Expand Up @@ -45,32 +46,8 @@ export function attachmentAdpator(
response.data.toString() === '[object Blob]'
? response.data
: new Blob([response.data], {type: type});
if (typeof (window.navigator as any).msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
(window.navigator as any).msSaveBlob(blob, filename);
} else {
let URL = window.URL || (window as any).webkitURL;
let downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
let a = document.createElement('a');
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
(window as any).location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
(window as any).location = downloadUrl;
}
setTimeout(function () {
URL.revokeObjectURL(downloadUrl);
}, 100); // cleanup
}

saveAs(blob, filename);
return {
...response,
data: {
Expand Down
12 changes: 2 additions & 10 deletions packages/amis/src/renderers/QRCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@ import {QRCode as QRCodeRender} from 'qrcode-react-next';
import {BaseSchema, SchemaClassName} from '../Schema';
import {getPropValue} from 'amis-core';
import mapValues from 'lodash/mapValues';
import {saveAs} from 'file-saver';

function downloadBlob(blob: Blob, filename: string) {
const objectUrl = URL.createObjectURL(blob);

const link = document.createElement('a');
link.href = objectUrl;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

setTimeout(() => URL.revokeObjectURL(objectUrl), 500);
return saveAs(blob, filename);
}

export interface QRCodeImageSettings {
Expand Down

0 comments on commit 86809d2

Please sign in to comment.