非常教程

Electron参考手册

电子下载 | DownloadItem

电子下载 | DownloadItem

从远程源下载控制文件。

程序:主

DownloadItemEventEmitter表示电子下载项的。它用于will-download事件Session类,并允许用户控制下载项。

// In the main process.
const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) => {
  // Set the save path, making Electron not to prompt a save dialog.
  item.setSavePath('/tmp/save.pdf')

  item.on('updated', (event, state) => {
    if (state === 'interrupted') {
      console.log('Download is interrupted but can be resumed')
    } else if (state === 'progressing') {
      if (item.isPaused()) {
        console.log('Download is paused')
      } else {
        console.log(`Received bytes: ${item.getReceivedBytes()}`)
      }
    }
  })
  item.once('done', (event, state) => {
    if (state === 'completed') {
      console.log('Download successfully')
    } else {
      console.log(`Download failed: ${state}`)
    }
  })
})

实例事件

事件:“更新”

返回:

  • event事件
  • state

在下载已更新且未完成时发出。

state可以是下列之一:

  • progressing-下载正在进行中。
  • interrupted-下载中断,可以恢复。

活动:“完成”

返回:

  • event事件
  • state

当下载处于终端状态时发射。这包括完成的下载,取消的下载(通过downloadItem.cancel())以及无法恢复的中断下载。

state可以是下列之一:

  • completed-下载工作顺利完成。
  • cancelled-下载已被取消。
  • interrupted-下载中断,无法恢复。

实例方法

downloadItem对象具有以下方法:

downloadItem.setSavePath(path)

  • pathString-设置下载项的保存文件路径。

API仅在会话的will-download回调函数中可用。如果用户没有通过API设置保存路径,Electron将使用原始程序确定保存路径(通常会提示保存对话框)。

downloadItem.getSavePath()

返回String- 下载项目的保存路径。这将是通过设置的路径downloadItem.setSavePath(path)或从显示的保存对话框中选择的路径。

downloadItem.pause()

暂停下载。

downloadItem.isPaused()

返回Boolean- 下载是否暂停。

downloadItem.resume()

恢复已暂停的下载。

注意:要启用可恢复的下载,您要下载的服务器必须支持范围请求并提供两者Last-ModifiedETag标题值。否则resume()将解除先前接收的字节并从头开始重新开始下载。

downloadItem.canResume()

恢复Boolean- 是否可以恢复下载。

downloadItem.cancel()

取消下载操作。

downloadItem.getURL()

返回String- 从中下载项目的源URL。

downloadItem.getMimeType()

返回String- MIME类型的文件。

downloadItem.hasUserGesture()

返回Boolean- 下载是否具有用户手势。

downloadItem.getFilename()

返回String- 下载项目的文件名称。

注意:文件名不总是与本地磁盘中保存的实际文件名相同。如果用户在提示的下载保存对话框中更改文件名称,保存文件的实际名称将会不同。

downloadItem.getTotalBytes()

返回Integer- 下载项目的总大小(以字节为单位)。

如果大小未知,则返回0。

downloadItem.getReceivedBytes()

返回Integer- 下载项目的接收字节数。

downloadItem.getContentDisposition()

返回String- 响应头中的Content-Disposition字段。

downloadItem.getState()

返回String- 当前状态。可以progressingcompletedcancelledinterrupted

注意:以下方法特别适用于cancelled会话重新启动时恢复项目。

downloadItem.getURLChain()

返回String[]- 包含任何重定向的项目的完整url链。

downloadItem.getLastModifiedTime()

返回String- Last-Modified标题值。

downloadItem.getETag()

返回String- ETag标题值。

downloadItem.getStartTime()

返回Double- 自下载开始时的UNIX纪元以来的秒数。

电子下载 | DownloadItem相关

Electron

Electron 是一个使用 JavaScript, HTML 和 CSS 等 Web 技术创建原生程序的框架,它负责比较难搞的部分,你只需把精力放在你的应用的核心上即可。

主页 https://electron.atom.io/
源码 https://github.com/electron/electron
发布版本 1.7.9

Electron目录

1.指南 | Guides
2.指南·开发 | Guides: Development
3.Webview 组件 |
4.加速器 | Accelerator
5.API
6.API·对象 | API·Objects
7.app
8.autoUpdater
9.浏览器总览 | BrowserView
10.浏览器窗口 | BrowserWindow
11.浏览器窗口代理 | BrowserWindowProxy)
12.ClientRequest
13.剪贴板 | clipboard
14.内容追踪 | contentTracing
15.小型文字档案 | Cookies
16.crashReporter
17.调试器 | Debugger
18.desktopCapturer
19.dialog
20.电子下载 | DownloadItem
21.环境变量 | Environment Variables
22.无框窗口 | Frameless Window
23.globalShortcut
24.IncomingMessage
25.ipcMain
26.IPC渲染器 | ipcRenderer
27.语言环境 | Locales
28.画面 | Menu
29.MenuItem
30.本地图像 | nativeImage
31.net
32.通知 | Notification
33.权限监控 | powerMonitor
34.权限存储拦截器 | powerSaveBlocker
35.处理 | process
36.协议 | protocol
37.远程 | remote
38.sandbox
39.屏幕 | screen
40.会话 | session
41.shell
42.系统表现 | systemPreferences
43.触摸板 | TouchBar
44.触摸板按钮 | TouchBarButton
45.触摸板颜色选择器 | TouchBarColorPicker
46.触摸板组 | TouchBarGroup
47.触摸板标签 | TouchBarLabel
48.触摸板弹出框 | TouchBarPopover
49.触摸板清理 | TouchBarScrubber
50.触摸板分段控制 | TouchBarSegmentedControl
51.触摸板滑块 | TouchBarSlider
52.触摸板间隔 | TouchBarSpacer
53.Tray
54.网页内容 | webContents
55.网页框架 | webFrame
56.网页要求 | WebRequest
57.窗口开启 | window.open