I am sending to the browser a request to save a file with the file name.
The file name might include spaces, so i replace all spaces with %20.
Internet Explorer and Chrome transfers %20 back to spaces, but Firefox does not to that. why?
Is there a way make all browsers show the space?
This is my code:
String codedName = new String(URLEncoder.encode(name, "UTF-8"));
codedName = codedName.replaceAll("\\+", "%20");
response.setHeader("Content-Disposition", "attachment; filename=\"" + codedName+ "\"");
That depends on how you create the file name. Usually, you can simply set the file name in the header field and the framework will encode it properly. In your case, you seem to encode the name twice. Try without encoding it.
You may use Javascript to encode the url.
The Syntax for encoding URL's in JavaScript is:
encodeURI(uri)
So, the code would be: (Note the space in-between my and test.)
<script type="text/javascript">
var uri="my test.html?name=jason&age=25";
document.write(encodeURI(uri)+ "<br />");
</script>
Which results in:
my%20test.html?name=jason&age=25
As per your recent comment "How do I do it in Java?"
The syntax would be something like:
encode(String s)
A simple Google search would reveal more information.
Related
Given this piece of code:
<%
Response.Write Server.URLEncode("a doc file.asp")
%>
It output this for a while (like Javascript call encodeURI):
a%20doc%20file.asp
Now, for unknow reason, I get:
a+doc+file%2Easp
I'm not sure of what I touched to make this happen (maybe the file content encoding ANSI/UTF-8). Why did this happen and how can I get the first behavior of Server.URLEncode, ie using a percent encoding?
Classic ASP hasn't been updated in nearly 20 years, so Server.URLEncode still uses the RFC-1866 standard, which specifies spaces be encoded as + symbols (which is a hangover from an old application/x-www-form-urlencoded media type), you must be mistaken in thinking it was encoding spaces as %20 at some point, not unless there's an IIS setting you can change that I'm unaware of.
More modern languages use the RFC-3986 standard for encoding URLs, which is why Javascript's encodeURI function returns spaces encoded as %20.
Both + and %20 should be treated exactly the same when decoded by any browser thanks to RFC backwards compatibility, but it's generally considered best to use %20 when encoding spaces in a URL as it's the more modern standard now, and some decoding functions (such as Javascript's decodeURIComponent) won't recognise + symbols as spaces and will fail to properly decode URLs that use them over %20.
You can always use a custom function to encode spaces as %20:
function URL_encode(ByVal url)
url = Server.URLEncode(url)
url = replace(url,"+","%20")
URL_encode = url
end function
I am building a url in a view within my sinatra app
I required the URI module in my app.
<li>Show</li>
Without URI.unescape I do not see %20 when I hover over the link. I just see this:
http://127.0.0.1:9292/blog/Coffee Title/49459
I am hoping for that space to be a -. But when I click on the link it will return in my browser as:
http://127.0.0.1:9292/blog/Coffee%20Title/49459
I tried using URI.unescape in irb. I am having trouble evaluating the Ruby code within the string. I am not sure what the right format is but think I am getting close.
URI.escape replaces characters in a string with their percent-encoded counterpart, which is why a space character becomes %20 (20 is the ASCII character code for space). It does not replace spaces with dashes.
To replace a character with another character in a string, use String#tr:
title = "Coffee Title"
title_with_dashes = title.tr(" ", "-")
puts title_with_dashes
# => Coffee-Title
However, this is only half of the equation. You've changed the URLs in your links but as a consequence your server probably isn't going to recognize the URL when someone clicks on the link, because the path that exists is Coffee Title, not Coffee-Title. That's a topic for another question, though.
I get the name of a file from a notification between one pluggin and my cocoa application. My problem is that I am receiving the file name like this: "My+file+name.png" instead of "My file name.png" (with spaces). I don't know how to decode this parameter in order to get the correct file name.
Any ideas? Thanks
Assuming that this is encoded as for a URL query string, you'll want to replace any plus signs with spaces and unescape the percent escape sequences.
I solved it in the javascript code from my pluggin. I added this function:
function decode(str) {
return unescape(str.replace(/\+/g, " "));
}
I called before passing the parameters to my cocoa application.
I have my database results (áéíóúàâêô...) and when I display any of this characters I get codes like:
á
My controller is like this:
ViewBag.EstadosDeAlma = (from e in db.EstadosDeAlma select e.Title).ToList();
My cshtml page is like this:
var data = '#foreach (dynamic item in ViewBag.EstadosDeAlma){ #(item + " ") }';
In addition, if I use any rich text editor as Tiny MCE all non-latin characters are like this too.
What should I do to avoid this problem?
What output encoding are you using on your web pages? I would suggest using UTF-8 since you want a lot of non-ascii characters to work.
I think you should HTML encode/decode the values before comparing them.
Since you are using jQuery you can take advantage of the encoding functions built-in into it. For example:
$('<div/>').html('& #225;gil').html()
gives you "ágil" (notice that I added an extra space between the & and the # so that stackoverflow does not encode it, you won't need it)
This other question has more information about this.
HTML-encoding lost when attribute read from input field
I'm serving a file from Lighttpd whose name contains space-characters. I'm using mimetype "application/octet-stream"
When I download this in Chrome, it works perfectly. But when I download in Firefox, the filename is truncated at the first space.
Is this to do with the mimetype? With some other lightty config? Or maybe something to do with the kind of space-character I'm using?
You need to urlencode your links. Spaces are easily misunderstood in URLS.
The url code for space is %20 or you can also use +
So for http://example.com/test file.jpg
You would use:
http://example.com/test%20file.jpg
or
http://example.com/test+file.jpg
I prefer to not use filenames with spaces in them.