NEW 174076
WKWebView doesn't have API for downloading resources
https://bugs.webkit.org/show_bug.cgi?id=174076
Summary WKWebView doesn't have API for downloading resources
Dan Saunders
Reported 2017-07-02 02:08:16 PDT
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
Radar WebKit Bug Importer
Comment 1 2017-07-03 00:53:31 PDT
Brady Eidson
Comment 2 2017-07-03 10:19:08 PDT
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
Comment 3 2017-07-07 00:17:06 PDT
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
Comment 4 2017-07-07 09:10:02 PDT
(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
Comment 5 2017-07-07 09:16:27 PDT
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
Comment 6 2017-07-07 09:17:50 PDT
(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"
Note You need to log in before you can comment on or make changes to this bug.