views:

82

answers:

2

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+ "\"");
+2  A: 

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.

Aaron Digulla
A: 

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.

lucifer