electron/docs/api/download-item.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

210 строки
6.3 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

2016-11-15 03:14:19 +03:00
## Class: DownloadItem
2015-09-20 14:08:31 +03:00
2016-04-22 20:30:49 +03:00
> Control file downloads from remote sources.
Process: [Main](../glossary.md#main-process)<br />
_This class is not exported from the `'electron'` module. It is only available as a return value of other methods in the Electron API._
2016-11-03 20:26:00 +03:00
`DownloadItem` is an [EventEmitter][event-emitter] that represents a download item in Electron.
2016-06-09 14:32:37 +03:00
It is used in `will-download` event of `Session` class, and allows users to
2015-09-20 14:08:31 +03:00
control the download item.
```js
2015-09-24 10:55:45 +03:00
// In the main process.
2018-09-13 19:10:51 +03:00
const { BrowserWindow } = require('electron')
2020-07-09 20:18:49 +03:00
const win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) => {
2015-09-24 10:55:45 +03:00
// Set the save path, making Electron not to prompt a save dialog.
2016-06-09 14:32:37 +03:00
item.setSavePath('/tmp/save.pdf')
item.on('updated', (event, state) => {
2016-06-09 14:57:29 +03:00
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()}`)
}
2016-06-09 14:32:37 +03:00
}
})
2016-06-09 14:57:29 +03:00
item.once('done', (event, state) => {
if (state === 'completed') {
2016-06-09 14:32:37 +03:00
console.log('Download successfully')
2015-09-20 14:08:31 +03:00
} else {
2016-06-09 14:32:37 +03:00
console.log(`Download failed: ${state}`)
2015-09-20 14:08:31 +03:00
}
2016-06-09 14:32:37 +03:00
})
})
2015-09-20 14:08:31 +03:00
```
### Instance Events
#### Event: 'updated'
2015-09-20 14:08:31 +03:00
2016-06-09 14:32:37 +03:00
Returns:
* `event` Event
* `state` string - Can be `progressing` or `interrupted`.
2016-06-09 14:32:37 +03:00
Emitted when the download has been updated and is not done.
The `state` can be one of following:
* `progressing` - The download is in-progress.
* `interrupted` - The download has interrupted and can be resumed.
2015-09-20 14:08:31 +03:00
#### Event: 'done'
2016-06-09 14:32:37 +03:00
Returns:
* `event` Event
* `state` string - Can be `completed`, `cancelled` or `interrupted`.
2015-09-20 14:08:31 +03:00
2016-06-09 14:32:37 +03:00
Emitted when the download is in a terminal state. This includes a completed
2016-11-03 20:26:00 +03:00
download, a cancelled download (via `downloadItem.cancel()`), and interrupted
2015-09-20 14:08:31 +03:00
download that can't be resumed.
2016-06-09 14:32:37 +03:00
The `state` can be one of following:
* `completed` - The download completed successfully.
* `cancelled` - The download has been cancelled.
* `interrupted` - The download has interrupted and can not resume.
### Instance Methods
2015-09-20 14:08:31 +03:00
The `downloadItem` object has the following methods:
#### `downloadItem.setSavePath(path)`
2015-09-24 10:55:45 +03:00
* `path` string - Set the save file path of the download item.
2015-09-24 10:55:45 +03:00
The API is only available in session's `will-download` callback function.
If `path` doesn't exist, Electron will try to make the directory recursively.
2015-09-24 10:55:45 +03:00
If user doesn't set the save path via the API, Electron will use the original
routine to determine the save path; this usually prompts a save dialog.
#### `downloadItem.getSavePath()`
2016-07-28 03:33:36 +03:00
Returns `string` - The save path of the download item. This will be either the path
2016-07-28 03:33:36 +03:00
set via `downloadItem.setSavePath(path)` or the path selected from the shown
save dialog.
#### `downloadItem.setSaveDialogOptions(options)`
* `options` SaveDialogOptions - Set the save file dialog options. This object has the same
properties as the `options` parameter of [`dialog.showSaveDialog()`](dialog.md).
This API allows the user to set custom options for the save dialog that opens
for the download item by default.
The API is only available in session's `will-download` callback function.
#### `downloadItem.getSaveDialogOptions()`
Returns `SaveDialogOptions` - Returns the object previously set by `downloadItem.setSaveDialogOptions(options)`.
#### `downloadItem.pause()`
2015-09-20 14:08:31 +03:00
Pauses the download.
#### `downloadItem.isPaused()`
2016-06-09 14:51:01 +03:00
Returns `boolean` - Whether the download is paused.
2016-06-09 14:51:01 +03:00
#### `downloadItem.resume()`
2015-09-20 14:08:31 +03:00
Resumes the download that has been paused.
2017-11-23 15:31:09 +03:00
**Note:** To enable resumable downloads the server you are downloading from must support range requests and provide both `Last-Modified` and `ETag` header values. Otherwise `resume()` will dismiss previously received bytes and restart the download from the beginning.
#### `downloadItem.canResume()`
2016-06-09 14:51:01 +03:00
Returns `boolean` - Whether the download can resume.
2016-06-09 14:51:01 +03:00
#### `downloadItem.cancel()`
2015-09-20 14:08:31 +03:00
Cancels the download operation.
#### `downloadItem.getURL()`
2015-09-20 14:08:31 +03:00
Returns `string` - The origin URL where the item is downloaded from.
2015-09-20 14:08:31 +03:00
#### `downloadItem.getMimeType()`
2015-09-20 14:08:31 +03:00
Returns `string` - The files mime type.
2015-09-20 14:08:31 +03:00
#### `downloadItem.hasUserGesture()`
2015-09-20 14:08:31 +03:00
Returns `boolean` - Whether the download has user gesture.
2015-09-20 14:08:31 +03:00
#### `downloadItem.getFilename()`
2015-09-20 14:08:31 +03:00
Returns `string` - The file name of the download item.
2015-09-20 14:08:31 +03:00
**Note:** The file name is not always the same as the actual one saved in local
disk. If user changes the file name in a prompted download saving dialog, the
actual name of saved file will be different.
2015-09-20 14:08:31 +03:00
#### `downloadItem.getCurrentBytesPerSecond()`
Returns `Integer` - The current download speed in bytes per second.
#### `downloadItem.getTotalBytes()`
2015-09-20 14:08:31 +03:00
Returns `Integer` - The total size in bytes of the download item.
If the size is unknown, it returns 0.
2015-09-20 14:08:31 +03:00
#### `downloadItem.getReceivedBytes()`
2015-09-20 14:08:31 +03:00
Returns `Integer` - The received bytes of the download item.
#### `downloadItem.getPercentComplete()`
Returns `Integer` - The download completion in percent.
#### `downloadItem.getContentDisposition()`
Returns `string` - The Content-Disposition field from the response
header.
2016-06-09 14:32:37 +03:00
#### `downloadItem.getState()`
2016-06-09 14:32:37 +03:00
Returns `string` - The current state. Can be `progressing`, `completed`, `cancelled` or `interrupted`.
2016-11-23 11:35:26 +03:00
**Note:** The following methods are useful specifically to resume a
`cancelled` item when session is restarted.
#### `downloadItem.getURLChain()`
Returns `string[]` - The complete URL chain of the item including any redirects.
2016-11-23 11:35:26 +03:00
#### `downloadItem.getLastModifiedTime()`
Returns `string` - Last-Modified header value.
2016-11-23 11:35:26 +03:00
#### `downloadItem.getETag()`
Returns `string` - ETag header value.
2016-11-23 11:35:26 +03:00
#### `downloadItem.getStartTime()`
Returns `Double` - Number of seconds since the UNIX epoch when the download was
started.
#### `downloadItem.getEndTime()`
Returns `Double` - Number of seconds since the UNIX epoch when the download ended.
### Instance Properties
#### `downloadItem.savePath`
A `string` property that determines the save file path of the download item.
The property is only available in session's `will-download` callback function.
If user doesn't set the save path via the property, Electron will use the original
routine to determine the save path; this usually prompts a save dialog.
[event-emitter]: https://nodejs.org/api/events.html#events_class_eventemitter