问题描述:

This question already has an answer here:

  • Javascript - How to extract filename from a file input control

    8 answers

网友答案:

The C:\fakepath\ is simply something that is added in by the browser for security purposes andshouldn't be present when the actual values are posted to the server.

If you wanted to replaced them for display in your alert() call (or for other purposes), you could just try replacing it via a replace() call:

// This should strip off 'C:\fakepath\' from your file path
var ab = document.getElementById("myfile").value.replace('C:\\fakepath\\','');
alert(ab);

You can see a complete working example below (just add a file) :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Fakepath Eraser Pro</title>
</head>
<body>
  <input type='file' onchange='alert(this.value.replace("C:\\fakepath\\",""))' />
</body>
</html>
网友答案:

You could try replace?

var ab = document.getElementById("myfile").value.replace(/C:\\fakepath\\/i, '');
网友答案:

"\" is a special character and it has to be escaped in strings to work properly.

var path = "C:\\fakepath\\abc.pdf";
var array = path.split("\\");
console.log(array[array.length-1]);
// output will be "abc.pdf"
相关阅读:
Top