From faec36710e7b9df1eba0eb655e10f44e3237b2d9 Mon Sep 17 00:00:00 2001 From: purocean Date: Thu, 23 Dec 2021 22:45:36 +0800 Subject: [PATCH] feat: add `ctx.utils.downloadContent` method --- src/renderer/utils/index.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/renderer/utils/index.ts b/src/renderer/utils/index.ts index b0dbdfa3..2785194f 100644 --- a/src/renderer/utils/index.ts +++ b/src/renderer/utils/index.ts @@ -76,6 +76,14 @@ export function objectInsertAfterKey (obj: {}, key: string, content: {}) { return Object.fromEntries(items) } +export function downloadContent (filename: string, content: Buffer | string, type = 'application/octet-stream') { + const blob = new Blob([content], { type }) + const link = document.createElement('a') + link.href = window.URL.createObjectURL(blob) + link.download = filename + link.click() +} + export function md5 (content: any) { return CryptoJS.MD5(content).toString() }