filename passed to chrome.downloads.download API is ignored

Viewed 1313

I'm trying to initiate a file download in a Chrome extension's background js using the chrome.downloads.download API:

chrome.runtime.onMessage.addListener(
  function (arg) {
    chrome.downloads.download({
      url: arg[0],
      filename: arg[1] + ".jpg",
      conflictAction: "uniquify",
      saveAs: false
    });
  });

However the filename being specified isn't respected - the original file name is being used instead.

I've tested the same script on Firefox and it's working as expected there, but not Chrome.

I know there's also the chrome.downloads.onDeterminingFilename event that I can register a listener to and override the filename from there, but it doesn't fit my use case since the file name I'd like to use is passed in to the onMessage listener.

Not sure if there's anything obvious that I missed. Thanks!

**** Update ****

As I later realized, the issue may be due to some other mandatory Chrome extension that's also present since I'm using a corp laptop.

2 Answers

I would recommend using console.log(typeof arg[1]); before the chrome.downloads.download API call to confirm it is a string. You could also try using a template literal to create the file name.

filename: `${arg[1]}.jpg`,
Related