Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 统一文件下载逻辑 #11658

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading