OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

chrome.downloads API: Replace invalid characters in filename with regex

  • Thread starter Thread starter Bernard
  • Start date Start date
B

Bernard

Guest
I am trying to download files with the chrome.downloads.download(...). The filename is given externally, so I don't know the characters inside. If it contains invalid characters, the function download will throw an error Error: Invalid filename.


  • Is there a regex in JavaScript that replaces all the invalid starting/middle/ending Unicode characters with _ in the filename?


  • Or is there a documentation listing the rules for a filename in Chrome?


  • Is there a way to make Chrome replace invalid characters in my filename, instead of throwing an error?

Chrome disallows more characters than common filesystems (e.g. NTFS), and I am not sure the exact definition of "invalid character" by Chrome. My current regex attempt is

Code:
var regex = /^\.|\.$|[\x00-\x1f\\\/:*?"<>|\r\n\u200D]/g;
filename.replaceAll(regex, '_');

But it only covers a few of the invalid Unicode characters.

I avoid using the <a> method to download (i.e. create <a> with href and download attributes, then click on it), because I would like to create subdirectries in the downloads folder.

<p>I am trying to download files with the <code>chrome.downloads.download(...)</code>. The filename is given externally, so I don't know the characters inside. If it contains invalid characters, the function <code>download</code> will throw an error <code>Error: Invalid filename</code>.</p>
<ul>
<li><p>Is there a regex in JavaScript that replaces <strong>all</strong> the invalid starting/middle/ending Unicode characters with <code>_</code> in the filename?</p>
</li>
<li><p>Or is there a documentation listing the rules for a filename in Chrome?</p>
</li>
<li><p>Is there a way to make Chrome replace invalid characters in my filename, instead of throwing an error?</p>
</li>
</ul>
<p>Chrome disallows more characters than common filesystems (e.g. NTFS), and I am not sure the exact definition of "invalid character" by Chrome. My current regex attempt is</p>
<pre class="lang-js prettyprint-override"><code>var regex = /^\.|\.$|[\x00-\x1f\\\/:*?"<>|\r\n\u200D]/g;
filename.replaceAll(regex, '_');
</code></pre>
<p>But it only covers a few of the invalid Unicode characters.</p>
<p>I avoid using the <code><a></code> method to download (i.e. create <code><a></code> with <code>href</code> and <code>download</code> attributes, then click on it), because I would like to create subdirectries in the downloads folder.</p>
 
Top