Skip to content

Instantly share code, notes, and snippets.

@Semigradsky
Last active August 29, 2015 14:22
Show Gist options
  • Save Semigradsky/d3dd121088715cb70d68 to your computer and use it in GitHub Desktop.
Save Semigradsky/d3dd121088715cb70d68 to your computer and use it in GitHub Desktop.
fakepath

See about this

For historical reasons, the value IDL attribute prefixes the file name with the string "C:\fakepath". Some legacy user agents actually included the full path (which was a security vulnerability). As a result of this, obtaining the file name from the value IDL attribute in a backwards-compatible way is non-trivial. The following function extracts the file name in a suitably compatible manner:

function extractFilename(path) { if (path.substr(0, 12) == "C:\fakepath\") return path.substr(12); // modern browser var x; x = path.lastIndexOf('/'); if (x >= 0) // Unix-based path return path.substr(x+1); x = path.lastIndexOf('\'); if (x >= 0) // Windows-based path return path.substr(x+1); return path; // just the file name }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment