admin管理员组

文章数量:1414628

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: ".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.

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./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.

Share Improve this question edited Oct 4, 2021 at 23:40 ted 14.8k10 gold badges68 silver badges113 bronze badges asked Jun 21, 2015 at 0:34 chrmchrm 1,2881 gold badge11 silver badges16 bronze badges 7
  • 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 …) – C3roe Commented Jun 21, 2015 at 0:54
  • I would anticipate this would be the same as the typical ones. Take a look at this post: stackoverflow./questions/4814040/…. 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 Commented Jun 21, 2015 at 4:00
  • The filenames get automatically generated. I would rather give the user the choice what to do, than strip the problematic characters. – chrm Commented Jun 21, 2015 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 Commented Jun 21, 2015 at 10:48
  • 1 On Linux the opposite is the case. The API doesn't happily consume the filename. The error doesn't e from the filesystem, because all the characters are permitted. – chrm Commented Jun 21, 2015 at 16:41
 |  Show 2 more ments

1 Answer 1

Reset to default 4

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 mon strategy is to replace the characters with something allowed; for example, _

See also this question.

本文标签: javascriptProblematic characters for filename in chromedownloadsdownloadStack Overflow