星期一, 4月 02, 2012

[Javascript] 取得完整路徑的檔名

今天在測試input box的檔案路徑時,
發現不同瀏覽器的取出的值不太一致,Chrome的是完整路徑,FF跟IE都是檔名。
要取得檔案的話可以使用以下的javascript範例,
取自 "Javascript - How to extract filename from a file input control"


To split the string ({filepath}/{filename}) and get the file name you could use something like this:
str.split(/(\\|\/)/g).pop()
"The pop method removes the last element from an array and returns that value to the caller." MOZILLA DEVELOPER NETWORK
Example:
from: "/home/user/file.txt".split(/(\\|\/)/g).pop()
you get: "file.txt"


沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails