Our web application uses hyperlinks to reference downloadable documents. An issue recently arose where an apostrophe in the filename was breaking the href
link, preventing it from being downloaded.
So I decided to encode the URL with HttpUtility.UrlEncode
so that apostrophes are encoded as %27
. In the process, spaces were encoded as +
's like so:
http://www.whatever.com/docs/My+Doc+With+Spaces.xls
Whenever the URL is accessed via the link or copied and pasted into Chrome or Firefox's address bar, I get a 404. However, when I manually replace the +
's with a space and hit enter, the document downloads as expected. Oddly, immediately after the download, the URL reverts back to its original format using +
's which results in another 404 when I refresh the browser. Don't get it... the browser doesn't like the +
, but then decides that it does?
If I use HttpUtility.UrlPathEncode
then it will encode the spaces as %20
which results in a working URL, but it doesn't encode the apostrophe, so I am back at square one.
I need the best of both worlds here.
So my questions are:
- How can I encode a document filename that has spaces and apostrophes (and all other legal filename characters in a Windows OS)
- Why won't a hyperlink with
+
's work when opened directly? How come when I replace with spaces, the browser reverts it back to use+
's?