Files and data

Create a conservative display filename

For a generated download label, use a conservative filename policy and a fallback. Replacing problematic characters can improve portability, but it is not a substitute for server-side path validation.

Worked example

Node.js example · save as example.mjs and run node example.mjs

function downloadName(value) {
  const base = value.replace(/[\\/:*?"<>|\u0000-\u001f]/g, "_").replace(/[. ]+$/g, "").slice(0, 80);
  return base || "download";
}
console.log(downloadName("report/June?.csv"));
console.log(downloadName("..."));

How it works

The example replaces a deliberately broad set of separator and control characters and removes trailing periods and spaces. It also prevents an empty result. This gives a practical human-facing label for a browser-generated file.

Limits and mistakes

Reserved names, Unicode normalization, total path length, and platform-specific rules still need separate handling. Never concatenate the result into a privileged filesystem path and assume it establishes containment. The browser can also adjust a suggested download name.

Check the result

Expect report_June_.csv and download. Test long input, separators from both major path conventions, and an all-invalid name. Validate storage paths independently.

Sources

Reference documentation for the behavior used in this guide: