6

Using certain characters in the filename argument to the function chrome.downloads.download leads to an "Invalid filename" error, when starting the download. I can't find any information in the documentation and replacing for example : with %3A or &#58 ; doesn't work.

The problematic characters are:

: " ? ~ < > * |

Here is an example you can use in the background page console from any extension with the downloads permission.

chrome.downloads.download(
    {url: "http://i.imgur.com/3cWNMt3.webm",
     filename: "title:subtitle.webm"},
    function (downloadId) {
        if (downloadId===undefined)
            console.log(chrome.runtime.lastError);
        else
            console.log("Ok");
});

Is there a way to use these problematic characters?

Edit: Is there a list for the characters chrome.downloads.download doesn't support?

Edit 2: To put it another way. The user can manually download a file in Chrome (Linux) and, in the download dialog, name it:

title:subtitle.extension

I would like to do the same in my extension.

This filename is just an example, the filenames get generated automatically depending on the webpage and some user generated rules.

ted
  • 13,596
  • 9
  • 65
  • 107
chrm
  • 1,258
  • 1
  • 11
  • 16
  • Why would you want to use those characters in download file names? They are problematic in most file systems too. (Not all of them in all file systems – but since you don’t know what OS/file systems the users of your extension(?) will be using …) – CBroe Jun 21 '15 at 00:54
  • I would anticipate this would be the same as the typical ones. Take a look at this post: http://stackoverflow.com/questions/4814040/allowed-characters-in-filename. There really isn't a way to use these, filenames are basically native so you can't just encode them like a url, because there's no place to decode them on the other end. – Brian Jun 21 '15 at 04:00
  • The filenames get automatically generated. I would rather give the user the choice what to do, than strip the problematic characters. – chrm Jun 21 '15 at 10:46
  • I'm using Ubuntu and if I download a file with chrome I can name it ": " ? ~ < > * |.png" and it downloads just fine. – chrm Jun 21 '15 at 10:48
  • Your edited question makes little sense. The API itself will happily consime almost everything (there's a small list of exceptions in the docs like `..`), but then the underlying filesystem/OS can refuse some names. Chrome does not know about it in advance. – Xan Jun 21 '15 at 11:54
  • 1
    On Linux the opposite is the case. The API doesn't happily consume the filename. The error doesn't come from the filesystem, because all the characters are permitted. – chrm Jun 21 '15 at 16:41
  • Interesting information. Still, if you're looking for a cross-platform safe list of characters, see the link at the end of my question. – Xan Jun 21 '15 at 17:37

1 Answers1

3

Is there a way to use these problematic characters?

No. That would be an invalid filename.

What exactly is invalid varies by OS. Here's a full ruleset for Windows.

A common strategy is to replace the characters with something allowed; for example, _

See also this question.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206