Support web downloads in browser adapter#2534
Conversation
There was a problem hiding this comment.
Looks like the file needs more code comments.
There was a problem hiding this comment.
Updated.
- Added comments around the Blob/object URL/anchor click implementation.
- Expanded
dio_web_adapterREADME to clarify that the browser schedules a download from ablob:URL, and that the returnedResponseonly means Dio fetched the bytes and dispatched the download click. - Documented browser-controlled limitations: actual file write, filename handling, user prompts, page security policy, CORS, HTTP protocol handling through XHR, and required browser APIs.
|
How will the browser react when triggered? Will a download task be scheduled by a blob request? I can only see the status update on the webpage itself, as the demonstration video doesn't reflect the browser's behavior. Also, I'd like to know if this has any limitations across Web protocol versions and different browsers. |
When triggered, Dio first finishes the XHR request as bytes, then creates a I validated this behavior with Chrome, Firefox, and Safari demos. The UI differs per browser: Chrome/Firefox show a clearer download entry/panel, while Safari may only show a subtle toolbar/download indicator, but the file is still saved. Limitations:
|
Code Coverage Report: Only Changed Files listed
Minimum allowed coverage is |
|
Coverage info doesn't seem to be valid somehow. Paths are missing from it. cc @kuhnroyal |
There was a problem hiding this comment.
Pull request overview
This PR adds Web support for Dio.download when using dio_web_adapter, enabling browser downloads by fetching response bytes through Dio and triggering a Blob-backed download via a temporary anchor click. It also updates documentation and adds browser-only tests to validate the new behavior and edge cases.
Changes:
- Implement
DioForBrowser.downloadby requestingResponseType.bytes, resolving the suggested filename, and triggering a browser download via a Blob URL. - Introduce a dedicated
download_triggerutility with test hooks and add comprehensive browser tests for download behavior, cancellation, and error paths. - Document Web-specific limitations/semantics and update changelogs and README docs accordingly.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| plugins/web_adapter/lib/src/dio_impl.dart | Implements DioForBrowser.download to fetch bytes and trigger a browser download. |
| plugins/web_adapter/lib/src/download_trigger.dart | Adds Blob/object-URL + anchor-click download trigger with test hooks. |
| plugins/web_adapter/test/download_test.dart | Adds browser-only tests covering download success, naming, cancellation, and trigger failures. |
| plugins/web_adapter/README.md | Documents how Web downloads work and their limitations. |
| plugins/web_adapter/CHANGELOG.md | Notes new Web Dio.download support. |
| dio/README.md | Documents Web-specific download semantics/limitations in main README. |
| dio/README-ZH.md | Adds Chinese documentation for Web download semantics/limitations. |
| dio/lib/src/dio.dart | Updates API docs for download to describe Web behavior and constraints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| response.headers | ||
| ..set('redirects', response.redirects.length.toString()) | ||
| ..set('uri', response.realUri.toString()); |
Fixes #2408
This PR adds Web support for
Dio.downloadindio_web_adapter.Previously,
DioForBrowser.downloadalways threwUnsupportedError, so browser users could not callDio.downloadat all. This change keeps the existing public API and implements browser downloads by fetching the response bytes through Dio, creating aBlobobject URL, and triggering a hidden anchor click.On Web,
savePathis interpreted as the browser suggested filename, not as a filesystem path.New Pull Request Checklist
mainbranch to avoid conflicts (via merge from master or rebase)CHANGELOG.mdin the corresponding packageAdditional context and info (if any)
Implementation details:
DioForBrowser.downloadnow supports browser downloads throughBlob+ object URL + hidden anchor click.String savePathis treated as the suggested filename on Web.FutureOr<String> Function(Headers)save path callbacks are supported after response headers are available.FileAccessMode.appendthrowsUnsupportedErroron Web.Web limitations are documented:
FileAccessMode.appendis unsupporteddeleteOnErroris a no-op on WeblengthHeadervalues are not used for Web progress totalsTest result
Chrome:
dio-web-download-chrome-incognito.mp4
Safair:
dio-web-download-safari.mp4
Firefox:
dio-web-download-firefox-private.mp4