3

The use of FileReference has a constraint on valid characters.
Error: Error #2087: The FileReference.download() file name contains prohibited characters.
This is fine since I guess the restriction comes from the underlying file system anyway

Is there such a things as a generic way to trim / replace all prohibited characters?

For clarity I am after something like:
var dirty:String = "Eat this !@##$%%^&&*()\/";.txt
var clean:String = dirty.replaceAllProhibitedCharacters();

I am not looking for OS specific regular expressions, but a cross platform solution.

MonoThreaded
  • 11,429
  • 12
  • 71
  • 102

2 Answers2

7

The list of disallowed characters does not change depending on the underlying OS, it is a fixed list. From the documentation for FileReference.download() the list of disallowed characters is:

/\:*?"<>|%

Edit: It looks like @ isn't allowed either.

If you want to remove those characters from an arbitrary string you can do something like this:

var validFileName:String = invalidFileName.replace(/[\/\\:*?"<>|%@]/g, "");

If you want to replace them with something else, then change the second parameter to replace().

Edit: added the @ character; escaped the / character.

mamapitufo
  • 4,680
  • 2
  • 25
  • 19
  • I am actually invoking FileReference.save() which throws the error above. For some reason I also had to replace '@' ... Thanks for your response – MonoThreaded Jan 19 '12 at 16:23
  • Heh, I just checked the live docs for Flex 4.6 and the `save()` method has the same forbidden character list as the `download()` method... Anyway, I'm glad it solved your problem. – mamapitufo Jan 19 '12 at 16:54
  • 1
    That regex doesn't work because some of the characters need to be escaped. Combining with the '@' needed as well, I use: var validFileName:String = invalidFileName.replace(/\[\/\\:\*\?"<>\|%@]/g, ""); – steve Oct 09 '13 at 19:28
  • @steve only the backslash needs to be escaped. The other characters have no special meaning inside of a character class. Thanks for spotting this! – mamapitufo Oct 11 '13 at 16:22
  • '/' must also be escaped. It's a closing char for regexp, so your code will not compile – user1875642 Jul 14 '15 at 12:37
3

The previous answer did not work for me. This is what did work (Using Flex 4.5):

public class FileNameSanitizer
{
public static function sanitize( fileName:String ):String
{
    var p:RegExp = /[:\/\\\*\?"<>\|%]/g;
    return fileName.replace( p, "" );
}
}

And the testcase to prove it:

import flexunit.framework.TestCase;

public class FileNameSanitizerTest extends TestCase
{
    public function FileNameSanitizerTest()
    {
    }

    public function testSanitize():void
    {
        assertEquals( "bla", FileNameSanitizer.sanitize( "bla" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla/foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla\\foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla:foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla*foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla?foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla\"foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla<foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla>foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla|foo" ) );
        assertEquals( "blafoo", FileNameSanitizer.sanitize( "bla%foo" ) );

        assertEquals( "", FileNameSanitizer.sanitize( "/\\:*?\"<>|%" ) );
    }

}
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211