クラス: DownloadItem
クラス: DownloadItem
リモートソースからのファイルのダウンロードを制御します。
Process: Main
This class is not exported from the 'electron' module. Electron API では、他のメソッドの戻り値としてのみ利用できます。
DownloadItem は EventEmitter を継承しており、Electron でのダウンロードアイテムを表します。 これは Session クラスの will-download イベントで使用されており、ユーザーがダウンロードアイテムを制御できるようにします。
// メインプロセス
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) => {
// 保存パスを設定して、Electronに保存ダイアログを出さないようにします。
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}`)
}
})
})
インスタンスイベント
イベント: 'updated'
戻り値:
eventEventstatestring -progressingかinterruptedにできます。
ダウンロードが更新され、まだ未完了であるときに発生します。
state は、次のいずれかになります。
progressing- ダウンロードが進行中です。interrupted- ダウンロードが中断されましたが、再開することができます。
イベント: 'done'
戻り値:
eventEventstatestring -completed、cancelledかinterruptedにできます。
ダウンロードが終了状態となったときに発生します。 これは完了したダウンロード、キャンセルされたダウンロード (downloadItem.cancel() によって) 、再開できる中断されたダウンロードを含みます。
state は、次のいずれかになります。
completed- ダウンロードが正常に完了しました。cancelled- ダウンロードがキャンセルされました。interrupted- ダウンロードが中断され、再開することができません。
インスタンスメソッド
downloadItem オブジェクトには以下のメソッドがあります
downloadItem.setSavePath(path)
pathstring - ダウロードアイテムを保存するファイルパスを設定します。
このAPIは、セッションの will-download コールバック関数でのみ利用可能です。 path が存在しない場合、Electron はディレクトリを再帰的に作成しようとします。 ユーザがこのAPIを経由して保存先のパスを設定しない場合、Electron は、保存先のパスを決定するために独自のルーチンを使用します。通常は保存ダイアログを表示します。
downloadItem.getSavePath()
戻り値 string - ダウンロードアイテムの保存先パス。 これは downloadItem.setSavePath(path) で設定されたものか、保存ダイアログで選択されたものになります。
downloadItem.setSaveDialogOptions(options)
この API によって、ダウンロードアイテムに対して既定で開かれる保存ダイアログのカスタムオプションをユーザが設定できます。 このAPIは、セッションの will-download コールバック関数でのみ利用可能です。
downloadItem.getSaveDialogOptions()
戻り値 SaveDialogOptions - downloadItem.setSaveDialogOptions(options)によってその前に設定されたオブジェクトを返す。
downloadItem.pause()
ダウンロードを一時停止します。
downloadItem.isPaused()
戻り値 boolean - ダウンロードが一時停止しているかどうか。
downloadItem.resume()
一時停止されたダウンロードを再開します。
[!NOTE] To enable resumable downloads the server you are downloading from must support range requests and provide both
Last-ModifiedandETagheader values. そうでなければ、resume()は、前回受信したバイト数を無視して、最初からダウンロードを再開します。
downloadItem.canResume()
戻り値 boolean - ダウンロードを再開できるかどうか。
downloadItem.cancel()
ダウンロード操作をキャンセルします。
downloadItem.getURL()
戻り値 string - アイテムがダウンロードされた元の URL。
downloadItem.getMimeType()
戻り値 string - ファイルの MIME タイプ。
downloadItem.hasUserGesture()
戻り値 boolean - ダウンロードにユーザージェスチャがあるかどうか。
downloadItem.getFilename()
戻り値 string - ダウンロードアイテムのファイル名。
[!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.
downloadItem.getCurrentBytesPerSecond()
戻り値 Integer - バイト毎秒単位の現在のダウンロード速度。
downloadItem.getTotalBytes()
戻り値 Integer - ダウンロードアイテムのバイト単位での合計サイズ。
サイズが不明な場合、0を返します。
downloadItem.getReceivedBytes()
戻り値 Integer - ダウンロードアイテムの受信したバイト数。
downloadItem.getPercentComplete()
戻り値 Integer - パーセント単位のダウンロード完了率。
downloadItem.getContentDisposition()
戻り値 string - レスポンスヘッダーの Content-Disposition フィールド。
downloadItem.getState()
戻り値 string - 現在の状態。 Can be progressing, completed, cancelled or interrupted.
[!NOTE] The following methods are useful specifically to resume a
cancelleditem when session is restarted.
downloadItem.getURLChain()
戻り値 string[] - すべてのリダイレクトを含むアイテムの完全な URL チェーン。
downloadItem.getLastModifiedTime()
戻り値 string - ヘッダーの Last-Modified の値。
downloadItem.getETag()
戻り値 string - ヘッダーの ETag の値。
downloadItem.getStartTime()
戻り値 Double - ダウンロードが開始されたUNIXエポックからの秒数。
downloadItem.getEndTime()
戻り値 Double - ダウンロードが終了した時の UNIX エポックからの秒数。
インスタンスプロパティ
downloadItem.savePath
string 型のプロパティです。ダウンロードアイテムを保存するファイルパスを決定します。
このプロパティは、セッションの will-download コールバック関数内でのみ利用可能です。 ユーザがこのプロパティを経由して保存先のパスを設定しない場合、Electron は、保存先のパスを決定するために独自のルーチンを使用します。通常は保存ダイアログを表示します。