DownloadsPort
DownloadsPort initiates, monitors, and manages file downloads, giving extensions full control over the browser's download queue.
Context
- Availability: Background
- Source:
packages/ports/src/background/downloads/downloads.port.ts
Key Methods
download(options: WebExtDownloadsDownloadOptions): Promise<number>- Start a download, returns the download ID.search(query: WebExtDownloadsQuery): Promise<WebExtDownloadItem[]>- Query download history.erase(query: WebExtDownloadsQuery): Promise<number[]>- Remove download records matching a query.pause(downloadId: number): Promise<void>- Pause an in-progress download.resume(downloadId: number): Promise<void>- Resume a paused download.cancel(downloadId: number): Promise<void>- Cancel an in-progress download.show(downloadId: number): Promise<void>- Reveal the downloaded file in the OS file manager.open(downloadId: number): Promise<void>- Open the downloaded file.
Usage
import { DownloadsPort } from '@hexajs-dev/ports';
import { Injectable, HexaContext } from '@hexajs-dev/common';
@Injectable({ context: HexaContext.Background })
export class ReportExportService {
constructor(private readonly downloads: DownloadsPort) {}
async exportReport(url: string, filename: string): Promise<number> {
const downloadId = await this.downloads.download({
url,
filename,
conflictAction: 'uniquify',
});
return downloadId;
}
async clearCompletedDownloads() {
await this.downloads.erase({ state: 'complete' });
}
}
API Reference Appendix
Below is the exhaustive, auto-generated technical reference for all types, interfaces, and signatures associated with this API.
Classes
DownloadsPort
import { DownloadsPort } from '@hexajs-dev/ports';
class DownloadsPort { ... }
Methods
cancel()
cancel(downloadId: number): Promise<void>
download()
download(options: HexaWebDownloadsDownloadOptions): Promise<number>
erase()
erase(query: HexaWebDownloadsQuery): Promise<number[]>
onChangedAddListener()
onChangedAddListener(listener: (delta: any) => void): void
onChangedRemoveListener()
onChangedRemoveListener(listener: (delta: any) => void): void
open()
open(downloadId: number): Promise<void>
pause()
pause(downloadId: number): Promise<void>
resume()
resume(downloadId: number): Promise<void>
search()
search(query: HexaWebDownloadsQuery): Promise<HexaWebDownloadItem[]>
show()
show(downloadId: number): Promise<void>