Downloading file with “;” or “#” in file name ruins filename

前端 未结 4 1728
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 20:20

I have a file named AttachmentDownload.aspx and inside Page_Load method have such code wich offers to download file. All names work correctly in IE except names which includ

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 21:15

    I started to just clean my filenames of all special characters. I've come up with a mapping table that should map most of the commonly used chars.

    //http://www.pjb.com.au/comp/diacritics.html
    private static string[,] CharacterReplacements = { 
        { " ", "-"},
        { "&", "-"},
        { "?", "-"},
        { "!", "-"},
        { "%", "-"},
        { "+", "-"},
        { "#", "-"},
        { ":", "-"},
        { ";", "-"},
        { ".", "-"},
    
        { "¢", "c" },   //cent
        { "£", "P" },   //Pound
        { "€", "E" },   //Euro
        { "¥", "Y" },   //Yen
        { "°", "d" },   //degree
        { "¼", "1-4" }, //fraction one-quarter
        { "½", "1-2" }, //fraction half    
        { "¾", "1-3" }, //fraction three-quarters}
        { "@", "AT)"}, //at                                                  
        { "Œ", "OE" },  //OE ligature, French (in ISO-8859-15)        
        { "œ", "oe" },  //OE ligature, French (in ISO-8859-15)        
    
        {"Å","A" },  //ring
        {"Æ","AE"},  //diphthong
        {"Ç","C" },  //cedilla
        {"È","E" },  //grave accent
        {"É","E" },  //acute accent
        {"Ê","E" },  //circumflex accent
        {"Ë","E" },  //umlaut mark
        {"Ì","I" },  //grave accent
        {"Í","I" },  //acute accent
        {"Î","I" },  //circumflex accent
        {"Ï","I" },  //umlaut mark
        {"Ð","Eth"}, //Icelandic
        {"Ñ","N" },  //tilde
        {"Ò","O" },  //grave accent
        {"Ó","O" },  //acute accent
        {"Ô","O" },  //circumflex accent
        {"Õ","O" },  //tilde
        {"Ö","O" },  //umlaut mark
        {"Ø","O" },  //slash
        {"Ù","U" },  //grave accent
        {"Ú","U" },  //acute accent
        {"Û","U" },  //circumflex accent
        {"Ü","U" },  //umlaut mark
        {"Ý","Y" },  //acute accent
        {"Þ","eth"}, //Icelandic - http://en.wikipedia.org/wiki/Thorn_(letter)
        {"ß","ss"},  //German
    
        {"à","a" },  //grave accent
        {"á","a" },  //acute accent
        {"â","a" },  //circumflex accent
        {"ã","a" },  //tilde
        {"ä","ae"},  //umlaut mark
        {"å","a" },  //ring
        {"æ","ae"},  //diphthong
        {"ç","c" },  //cedilla
        {"è","e" },  //grave accent
        {"é","e" },  //acute accent
        {"ê","e" },  //circumflex accent
        {"ë","e" },  //umlaut mark
        {"ì","i" },  //grave accent
        {"í","i" },  //acute accent
        {"î","i" },  //circumflex accent
        {"ï","i" },  //umlaut mark
        {"ð","eth"}, //Icelandic
        {"ñ","n" },  //tilde
        {"ò","o" },  //grave accent
        {"ó","o" },  //acute accent
        {"ô","o" },  //circumflex accent
        {"õ","o" },  //tilde
        {"ö","oe"},  //umlaut mark
        {"ø","o" },  //slash
        {"ù","u" },  //grave accent
        {"ú","u" },  //acute accent
        {"û","u" },  //circumflex accent
        {"ü","ue"},  //umlaut mark
        {"ý","y" },  //acute accent
        {"þ","eth"}, //Icelandic - http://en.wikipedia.org/wiki/Thorn_(letter)
        {"ÿ","y" },  //umlaut mark
        };
    

    Or you can get the whole code here:

    http://remy.supertext.ch/2012/08/clean-filenames/

    Let me know if you find chars that are missing.

提交回复
热议问题