Reputation: 175
I need to put a link with this href="file://attachments/aaaa_#_aaaa.msg"
Obviously in that way is not working because the hash character #
is used for anchors.
So I try to change this to: href="file://attachments/aaaa_%23_aaaa.msg"
but when I open the url in the IE, the browser is trying to open this: href="file://attachments/aaaa_%2523_aaaa.msg"
IE is encoding the %
character to %25
How can I put the file name in the URL to encode and read the hash character #
in all the browsers to download the file?
I can't change the file name to remove this character, so I need a way to deal with this problem.
Upvotes: 10
Views: 6955
Reputation: 52538
The problem is that in a URL, # introduces the fragment, and ? introduces the query. Path components on Unix / Linux / MacOS / iOS fortunately cannot contain "/" characters (they are separated by "/" but cannot contain a slash) because that would be another pain.
Almost all characters in the file path either become part of the URL unchanged, or they are percent-encoded and URLs know that for example %20 in the percent-encoded URL is really a space character in the path.
The two exceptions are # and ?. As soon as you have a path containing # or ?, which is perfectly legal, parsing the URL will think that "?" introduces a query, and "#" introduces a fragment, so ? or # and all following characters are not part of the path, but are turned into "query" and "fragment". And you cannot escape them in the original path, because %23 is again a perfectly legal path of three characters, percent, two and three, and gets percent-escaped to %2523, and when you or the OS tries to recreate the path, this is translated to %23.
(The "?" might cause trouble in shell scripting, but inside a path it is just as legal as any other character, at least in MacOS X and iOS. Even a nul byte is legal in a path except that C code trying to handle a path as a C string will again run into trouble).
Upvotes: 0
Reputation: 402
You will avoid lots and lots and lots of pain if you are able to rename your files so they don't contain a "#" character. As long as they do, you will probably have current and future cross-browser issues, confusion on behalf of future developers working on your code (or confusion on your behalf in the future, when you've forgotten the ins and outs of the encoding), etc. Also, some Unix/Linux systems don't allow "#" in filenames. Not sure what OS you're using, but your filenames should be as portable as possible across OSs, even if you're "sure" right now that you'll never be running on one of those systems.
Upvotes: 2