Using js to get the uploaded file name is purely for cosmetic purposes
- 2020-03-26 21:32:44
- OfStack
Why use js to get the uploaded file name? It's just for beautification, for friendliness,
However, instead of using path. Substring (path. LastIndexOf ('/')+1);
There are a lot of problems with this model when it works.
For example, the value of the input form in firefox gets the file name by default, while ie displays the file path.
So make a distinction: in firefox, lastIndexOf('/') gets -1, whereas in ie, the directory shows a backslash, \, and of course on Linux
Next, the forward slash/is obtained by other browsers (it has not been proved yet that the value obtained by browser file under Linux will be the full path);
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201310/201310231652281.gif? 2013923165457 ">
It has to be judged twice:
However, instead of using path. Substring (path. LastIndexOf ('/')+1);
There are a lot of problems with this model when it works.
For example, the value of the input form in firefox gets the file name by default, while ie displays the file path.
So make a distinction: in firefox, lastIndexOf('/') gets -1, whereas in ie, the directory shows a backslash, \, and of course on Linux
Next, the forward slash/is obtained by other browsers (it has not been proved yet that the value obtained by browser file under Linux will be the full path);
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201310/201310231652281.gif? 2013923165457 ">
It has to be judged twice:
function getFileName(path){
var pos1 = path.lastIndexOf('/');
var pos2 = path.lastIndexOf('\');
var pos = Math.max(pos1, pos2)
if( pos<0 )
return path;
else
return path.substring(pos+1);
}