tantangula
tantangula

Reputation: 314

Chrome.downloads.download in Chrome App

Chrome is telling me that chrome.downloads is undefined when I try to use it in my app.

Here is a simple example where I am trying to download an image...

Manifest:

{
  "manifest_version": 2,
  "name": "Downloader",
  "minimum_chrome_version": "38",
  "permissions": ["downloads", "<all_urls>"],
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  }
}

HTML:

<body>
  <button id="download">Download</button>
  <script src="main.js"></script>
</body>

Background.js

chrome.app.runtime.onLaunched.addListener(function(launchData) {
  chrome.app.window.create(
    'index.html',
    {
      id: 'mainWindow',
      bounds: {width: 800, height: 600}
    }
  );
});

Main.js

window.onload = function() {
  document.querySelector("#download").addEventListener("click",
    function () {
      chrome.downloads.download({
        url: "http://upload.wikimedia.org/wikipedia/commons/6/6e/Moonbeam_UFO.JPG",
        filename: "ufo.jpg"
      });
    }
  );
};

There are similar posts about trying to get the downloads object working when developing extensions, but I can't find anything about using it in an app. Has anyone been able to use this successfully, or am I doing something wrong?

Thanks

Upvotes: 2

Views: 9827

Answers (1)

Rob W
Rob W

Reputation: 348992

The chrome.downloads API is not yet supported in Chrome apps (crbug.com/274673).

But you can still trigger a file download with the standard APIs of the web platform. In fact, the logic from your question can completely be replicated with pure HTML:

<a download="ufo.jpg" href="http://upload.wikimedia.org/wikipedia/commons/6/6e/Moonbeam_UFO.JPG">Download</a>

(styling the anchor as a button is left as an exercise to the reader)

Upvotes: 2

Related Questions