Saliem Talash: When a filename becomes a download link

A download button can have the right label and still point somewhere other than the file its author intended. A filename such as release #2.txt is readable to a person. Placed directly after a website address, its hash character also has a job in URL syntax.

The useful handoff question is small: did the whole filename stay in the path, or did part of it become a query or fragment? Answer that before treating a working-looking link as a finished download.

Four names, one proposed route

I used four fictional filename strings to compare two ways of constructing a URL. The proposed route was https://example.invalid/files/. Nothing was uploaded and no request was sent to that address. These are logical input strings, not a claim that every operating system permits these filenames.

The local calculation ran on November 6, 2026 in Node.js 26.11.1. AI assistance was used to design the example, run the comparison and draft this article. It tests URL construction, not a live website, a browser download or a customer's files.

The first approach passed each name directly to the URL constructor with the base address. The second encoded the name as one component before resolving it against that same base. For each result, the comparison retained three separate values: pathname, search and hash.

MDN describes pathname as a structure made from path segments. Its documentation for encodeURIComponent() explains that the function encodes characters used in URI syntax. The distinction matters here: a name intended as data should not accidentally supply the structure around that data.

A hash can move part of the name out of the path

For the direct input release #2.txt, the observed values were:

pathname: /files/release%20
search:   empty
hash:     #2.txt

The space became %20, but #2.txt was a fragment. The pathname no longer contained the whole intended name. MDN's hash documentation identifies the hash property as the fragment identifier preceded by #.

Encoding the name first produced the component release%20%232.txt. In the constructed URL, the pathname was /files/release%20%232.txt, and both query and fragment were empty. Decoding that final segment once recovered the original string, including its space and hash.

That is a construction result. It does not show that a server has a matching file, that its route accepts the encoded name or that a download will start. Those questions remain for the actual destination.

A question mark creates a different split

The direct input questions?.txt produced a pathname of /files/questions and a search value of ?.txt. The question mark separated the query from the path. The URL Standard's parsing rules treat question marks and hashes as structural boundaries in this kind of URL.

With the filename encoded as one component, the pathname became /files/questions%3F.txt. The search and hash values were empty, and decoding the last segment once recovered questions?.txt.

A query is not inherently a mistake. Some download services deliberately use one to select a file. The mistake would be assuming that a filename belongs in the path while constructing a query instead. Confirm the route's contract before changing its encoding.

An existing percent sign needs its own interpretation

The third input was draft%20copy.txt. Here the three characters %20 were deliberately part of the fictional name. They did not stand for a space in the original input.

Passing that name directly produced /files/draft%20copy.txt. Decoding its final segment once yielded draft copy.txt, which was a different string. Encoding the original name first produced draft%2520copy.txt; decoding that constructed segment once recovered the intended literal %20.

This example explains why “remove the extra percent signs” is a poor repair instruction. First establish whether the input is an original name or an already encoded URL component. Encoding an already encoded component again can change its meaning. A visible %25 alone does not prove an error.

The fourth input, café.txt, acted as a useful comparison. Both approaches produced /files/caf%C3%A9.txt, with no query or fragment. One successful accented name would therefore have missed the structural problems exposed by the hash and question mark.

Keep the construction example small

For this demonstration's four original names, the construction step was:

const name = "release #2.txt";
const part =
  encodeURIComponent(name);
const url = new URL(
  part,
  "https://example.invalid/files/"
);

This encodes the proposed filename component, rather than encoding the complete address. It is an illustration for the stated route and inputs, not a universal filename-to-link utility. A real service may use generated identifiers, nested paths or a different API entirely.

The comparison checked that all four constructed results had empty query and fragment values and that decoding the final segment once recovered the respective original string. It did not test every Unicode string, server decoding behaviour, permissions, file contents or security. URL encoding does not establish any of those properties.

A download-link handoff that can be reviewed

A useful review record can fit in five fields:

  1. Original value: retain the exact name before URL encoding, including literal percent signs.
  2. Route contract: say whether the value belongs in one path segment, a query parameter or another identifier field.
  3. Constructed URL: keep the actual destination, separate from the button's readable label.
  4. Expected interpretation: record the intended path, query and fragment, including values expected to be empty.
  5. Delivery observation: on an authorised real destination, record which file actually arrived and under what access conditions.

The last field is intentionally unfinished in this local example. A round-trip string comparison is useful evidence for construction; it cannot substitute for opening the real destination and checking the delivered file. Preserve the original input and the observed URL parts when reporting a defect, so the next person can distinguish an encoding problem from a route or delivery problem.

Sources and author

Saliem Talash is a Toronto-based founder. Read his STEO journal and editorial approach. This article uses fictional inputs and makes no claim about customer results or measured audience demand.

Comments

Popular posts from this blog

Saliem Talash: Why I made a website project workbook that works offline

Saliem Talash: all official pages, videos and profiles

Saliem Talash: A worked website handoff for a fictional flower shop