user2457512
user2457512

Reputation: 21

%20 in filename, browser sees whitespaces. How to avoid that?

I have imported images to my own server. One of the many filenames has %20 in them, e.g. 50128%202789%20001V%20500.jpg. However the browser sees it as 50128 2789 001V 500.jpg, so it won't display the image.

What solution can I use, to display the image properly?

Upvotes: 2

Views: 1541

Answers (1)

Don Roby
Don Roby

Reputation: 41137

"%20" is a percent-encoding for a space, and has special meaning in a URL. Basically, you can't use spaces in a URI or URL, and have to replace them with a code to comply with the rules. Things that read URLs (the server for example) translate them back.

Unfortunately, that means if you have a filename containing this sequence, it will be mistaken for a percent-encoded space, as you're seeing.

The solution is to also percent-encode the '%'. The percent-encoding for a '%' character is "%25".

In your example, the name "50128%202789%20001V%20500.jpg" has to be encoded to "50128%25202789%2520001V%2520500.jpg" so that those '%' characters are not mistaken for spaces.

There are of course other things that get encoded. The rules are defined in the URI specification.

Upvotes: 2

Related Questions