222
schangxiang@126.com
2025-04-30 9bec4dcae002f36aa23231da11cb03a156b40110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//@ts-nocheck
export class Client {
  isClient() {
    return !!window.bridge?.electron
  }
  /**
   * 调用客户端主进程
   *
   * @example
   * // 最小化
   * invoke(function({ electron }, a, b){
   *   const { BrowserWindow } = electron
   *   BrowserWindow.getFocusedWindow()?.minimize()
   * }, 'a', 'b')
   *
   * @param fn 上下文独立,不可引用外部变量
   * @returns
   */
  async invoke(
    ...args: Parameters<NonNullable<typeof window.bridge>['invoke']>
  ) {
    if (!window.bridge) {
      throw new Error('!bridge')
    }
    return await window.bridge.invoke(...args)
  }
}
export class Download {
  /**
   * 下载文件
   * @param data
   * @param name
   * @param mediaType
   */
  file(data: any, name: string, mediaType?: string) {
    const client = new Client()
    // 客戶端默认保存类型错误,重新设置
    if (client.isClient()) {
      let arr = name.split('.')
      let fileType = arr[arr.length - 1]
      client.invoke(({ electron }, fileType) => {
        const { BrowserWindow, downloadItem } = electron
        const win = BrowserWindow.getFocusedWindow()
 
        win.webContents.session.on(
          'will-download',
          (event: Event, item: typeof downloadItem) => {
            item.setSaveDialogOptions({
              filters: [{ name: `${fileType} File`, extensions: [fileType] }],
            })
            //...
          }
        )
      }, fileType)
    }
 
    const blob = new Blob([data], { type: mediaType })
    // 创建下载链接
    const downloadHref = URL.createObjectURL(blob)
    // 创建a标签并为其添加属性
    const downloadLink = document.createElement('a')
    downloadLink.href = downloadHref
    downloadLink.download = name
    // 触发点击事件执行下载
    downloadLink.click()
    URL.revokeObjectURL(downloadHref)
  }
}