<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>174076</bug_id>
          
          <creation_ts>2017-07-02 02:08:16 -0700</creation_ts>
          <short_desc>WKWebView doesn&apos;t have API for downloading resources</short_desc>
          <delta_ts>2020-06-16 23:15:57 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit2</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>macOS 10.12.4</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Dan Saunders">dasau</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>achristensen</cc>
    
    <cc>aestes</cc>
    
    <cc>beidson</cc>
    
    <cc>cdumez</cc>
    
    <cc>dasau</cc>
    
    <cc>exextoc</cc>
    
    <cc>kc</cc>
    
    <cc>rasmus</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1324910</commentid>
    <comment_count>0</comment_count>
    <who name="Dan Saunders">dasau</who>
    <bug_when>2017-07-02 02:08:16 -0700</bug_when>
    <thetext>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 &quot;blob:https://localhost:3000/8acb6ff6-21d8-4345-afb6-9bf410bdd6ac&quot;, 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(&quot;click&quot;);
			node.dispatchEvent(event);

var save_link = doc.createElementNS(&quot;http://www.w3.org/1999/xhtml&quot;, &quot;a&quot;);
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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325054</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2017-07-03 00:53:31 -0700</bug_when>
    <thetext>&lt;rdar://problem/33102873&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325151</commentid>
    <comment_count>2</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2017-07-03 10:19:08 -0700</bug_when>
    <thetext>All &quot;possible download&quot; actions should go through the decidePolicyForNavigationAction API.

Is that not good enough? If not, please explain why.

Or, if it doesn&apos;t go through that API as expected, can you post a live example (attachment, jsfiddle, etc) that reproduces the bug?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1326411</commentid>
    <comment_count>3</comment_count>
    <who name="Dan Saunders">dasau</who>
    <bug_when>2017-07-07 00:17:06 -0700</bug_when>
    <thetext>Here is a basic repro:
https://jsfiddle.net/dsaunders45459/9h5yocvp/

var data = new Blob([&quot;test&quot;], {
  type: &apos;text/plain;charset=utf-8&apos;
});

var save_link = document.createElementNS(&quot;http://www.w3.org/1999/xhtml&quot;, &quot;a&quot;);
var object_url = window.URL.createObjectURL(data);
save_link.href = object_url;
save_link.download = &quot;testfile.txt&quot;;
var event = new MouseEvent(&quot;click&quot;);
save_link.dispatchEvent(event);


decidePolicyForNavigationAction gets called with &quot;blob:https://jsfiddle.net/7d0fb8d6-1a27-4b33-aaa0-9e17e4108b69&quot; 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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1326499</commentid>
    <comment_count>4</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2017-07-07 09:10:02 -0700</bug_when>
    <thetext>(In reply to Dan Saunders from comment #3)
&gt; Here is a basic repro:
&gt; https://jsfiddle.net/dsaunders45459/9h5yocvp/
&gt; 
&gt; var data = new Blob([&quot;test&quot;], {
&gt;   type: &apos;text/plain;charset=utf-8&apos;
&gt; });
&gt; 
&gt; var save_link = document.createElementNS(&quot;http://www.w3.org/1999/xhtml&quot;,
&gt; &quot;a&quot;);
&gt; var object_url = window.URL.createObjectURL(data);
&gt; save_link.href = object_url;
&gt; save_link.download = &quot;testfile.txt&quot;;
&gt; var event = new MouseEvent(&quot;click&quot;);
&gt; save_link.dispatchEvent(event);
&gt; 
&gt; 
&gt; decidePolicyForNavigationAction gets called with
&gt; &quot;blob:https://jsfiddle.net/7d0fb8d6-1a27-4b33-aaa0-9e17e4108b69&quot; URL, but
&gt; there is no way to download that file. The WebKit2 MiniBrowser does
&gt; decisionHandler(WKNavigationActionPolicyAllow) for this request and you can
&gt; see it results in a no-op.

I don&apos;t think the WK2 Minibrowsers supports downloads of any kind?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1326500</commentid>
    <comment_count>5</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2017-07-07 09:16:27 -0700</bug_when>
    <thetext>Sorry, I was misunderstanding the bug report.

I thought the report was along the lines of &quot;WK2 downloads things, but not blobs!&quot; which was a surprise to me.

In reality, the complaint is that &quot;There is no API for WKWebView apps to support downloads (of any resource)&quot;

That is true - download support is SPI.

Retitling to &quot;WKWebView doesn&apos;t have API for downloading resources&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1326501</commentid>
    <comment_count>6</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2017-07-07 09:17:50 -0700</bug_when>
    <thetext>(In reply to Chris Dumez from comment #4)
&gt; (In reply to Dan Saunders from comment #3)
&gt; &gt; 
&gt; &gt; decidePolicyForNavigationAction gets called with
&gt; &gt; &quot;blob:https://jsfiddle.net/7d0fb8d6-1a27-4b33-aaa0-9e17e4108b69&quot; URL, but
&gt; &gt; there is no way to download that file. The WebKit2 MiniBrowser does
&gt; &gt; decisionHandler(WKNavigationActionPolicyAllow) for this request and you can
&gt; &gt; see it results in a no-op.
&gt; 
&gt; I don&apos;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&apos;s not API is because there&apos;s no API to monitor and manage the download itself once you do a &quot;PolicyDownload&quot;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>