Bug 174076
Summary: | WKWebView doesn't have API for downloading resources | ||
---|---|---|---|
Product: | WebKit | Reporter: | Dan Saunders <dasau> |
Component: | WebKit2 | Assignee: | Nobody <webkit-unassigned> |
Status: | NEW | ||
Severity: | Normal | CC: | achristensen, aestes, beidson, cdumez, dasau, exextoc, kc, rasmus, webkit-bug-importer |
Priority: | P2 | Keywords: | InRadar |
Version: | WebKit Nightly Build | ||
Hardware: | Mac | ||
OS: | macOS 10.12.4 |
Dan Saunders
A popular library for download files in web browsers is https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js
The approach it takes is create a blob based resource such as "blob:https://localhost:3000/8acb6ff6-21d8-4345-afb6-9bf410bdd6ac", and then use the download attribute on the anchor to force the browser to download. We specifically use this technique to download an application/zip type file.
var click = function(node) {
var event = new MouseEvent("click");
node.dispatchEvent(event);
var save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
var object_url = get_URL().createObjectURL(blob);
setTimeout(function() {
save_link.href = object_url;
save_link.download = name;
click(save_link);
});
This was not working in Safari until the download attribute was implemented in 10.1 with https://bugs.webkit.org/show_bug.cgi?id=102914. I do not see any way to get the above code working in macOS WKWebView with the public delegate methods currently exposed to developers. We either need WKWebView to handle saving the file for us, or preferrably implement a new delegate that receives the blob contents and lets the application decide how to handle this scenario.
Attachments | ||
---|---|---|
Add attachment proposed patch, testcase, etc. |
Radar WebKit Bug Importer
<rdar://problem/33102873>
Brady Eidson
All "possible download" actions should go through the decidePolicyForNavigationAction API.
Is that not good enough? If not, please explain why.
Or, if it doesn't go through that API as expected, can you post a live example (attachment, jsfiddle, etc) that reproduces the bug?
Dan Saunders
Here is a basic repro:
https://jsfiddle.net/dsaunders45459/9h5yocvp/
var data = new Blob(["test"], {
type: 'text/plain;charset=utf-8'
});
var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
var object_url = window.URL.createObjectURL(data);
save_link.href = object_url;
save_link.download = "testfile.txt";
var event = new MouseEvent("click");
save_link.dispatchEvent(event);
decidePolicyForNavigationAction gets called with "blob:https://jsfiddle.net/7d0fb8d6-1a27-4b33-aaa0-9e17e4108b69" URL, but there is no way to download that file. The WebKit2 MiniBrowser does decisionHandler(WKNavigationActionPolicyAllow) for this request and you can see it results in a no-op.
Chris Dumez
(In reply to Dan Saunders from comment #3)
> Here is a basic repro:
> https://jsfiddle.net/dsaunders45459/9h5yocvp/
>
> var data = new Blob(["test"], {
> type: 'text/plain;charset=utf-8'
> });
>
> var save_link = document.createElementNS("http://www.w3.org/1999/xhtml",
> "a");
> var object_url = window.URL.createObjectURL(data);
> save_link.href = object_url;
> save_link.download = "testfile.txt";
> var event = new MouseEvent("click");
> save_link.dispatchEvent(event);
>
>
> decidePolicyForNavigationAction gets called with
> "blob:https://jsfiddle.net/7d0fb8d6-1a27-4b33-aaa0-9e17e4108b69" URL, but
> there is no way to download that file. The WebKit2 MiniBrowser does
> decisionHandler(WKNavigationActionPolicyAllow) for this request and you can
> see it results in a no-op.
I don't think the WK2 Minibrowsers supports downloads of any kind?
Brady Eidson
Sorry, I was misunderstanding the bug report.
I thought the report was along the lines of "WK2 downloads things, but not blobs!" which was a surprise to me.
In reality, the complaint is that "There is no API for WKWebView apps to support downloads (of any resource)"
That is true - download support is SPI.
Retitling to "WKWebView doesn't have API for downloading resources"
Brady Eidson
(In reply to Chris Dumez from comment #4)
> (In reply to Dan Saunders from comment #3)
> >
> > decidePolicyForNavigationAction gets called with
> > "blob:https://jsfiddle.net/7d0fb8d6-1a27-4b33-aaa0-9e17e4108b69" URL, but
> > there is no way to download that file. The WebKit2 MiniBrowser does
> > decisionHandler(WKNavigationActionPolicyAllow) for this request and you can
> > see it results in a no-op.
>
> I don't think the WK2 Minibrowsers supports downloads of any kind?
The first step is _WKNavigationActionPolicyDownload instead of WKNavigationActionPolicyAllow.
I (wrongly) thought PolicyDownload was API.
But of course the reason it's not API is because there's no API to monitor and manage the download itself once you do a "PolicyDownload"