How to get the file name from a full path using JavaScript?

前端 未结 18 939
生来不讨喜
生来不讨喜 2020-11-22 11:01

Is there a way that I can get the last value (based on the \'\\\' symbol) from a full path?

Example:

C:\\Documents and Settings\\img\\recycled log.jpg<

相关标签:
18条回答
  • 2020-11-22 11:53

    The complete answer is:

    <html>
        <head>
            <title>Testing File Upload Inputs</title>
            <script type="text/javascript">
    
            function replaceAll(txt, replace, with_this) {
                return txt.replace(new RegExp(replace, 'g'),with_this);
            }
    
            function showSrc() {
                document.getElementById("myframe").href = document.getElementById("myfile").value;
                var theexa = document.getElementById("myframe").href.replace("file:///","");
                var path = document.getElementById("myframe").href.replace("file:///","");
                var correctPath = replaceAll(path,"%20"," ");
                alert(correctPath);
            }
            </script>
        </head>
        <body>
            <form method="get" action="#"  >
                <input type="file"
                       id="myfile"
                       onChange="javascript:showSrc();"
                       size="30">
                <br>
                <a href="#" id="myframe"></a>
            </form>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 11:54
    var filename = fullPath.replace(/^.*[\\\/]/, '')
    

    This will handle both \ OR / in paths

    0 讨论(0)
  • 2020-11-22 11:55

    Little function to include in your project to determine the filename from a full path for Windows as well as GNU/Linux & UNIX absolute paths.

    /**
     * @param {String} path Absolute path
     * @return {String} File name
     * @todo argument type checking during runtime
     * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
     * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
     * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
     * @example basename('/home/johndoe/github/my-package/webpack.config.js') // "webpack.config.js"
     * @example basename('C:\\Users\\johndoe\\github\\my-package\\webpack.config.js') // "webpack.config.js"
     */
    function basename(path) {
      let separator = '/'
    
      const windowsSeparator = '\\'
    
      if (path.includes(windowsSeparator)) {
        separator = windowsSeparator
      }
    
      return path.slice(path.lastIndexOf(separator) + 1)
    }
    
    0 讨论(0)
  • 2020-11-22 11:56

    Not more concise than nickf's answer, but this one directly "extracts" the answer instead of replacing unwanted parts with an empty string:

    var filename = /([^\\]+)$/.exec(fullPath)[1];
    
    0 讨论(0)
  • 2020-11-22 11:56

    var file_name = file_path.substring(file_path.lastIndexOf('/'));

    0 讨论(0)
  • 2020-11-22 11:58
    <html>
        <head>
            <title>Testing File Upload Inputs</title>
            <script type="text/javascript">
                <!--
                function showSrc() {
                    document.getElementById("myframe").href = document.getElementById("myfile").value;
                    var theexa = document.getElementById("myframe").href.replace("file:///","");
                    alert(document.getElementById("myframe").href.replace("file:///",""));
                }
                // -->
            </script>
        </head>
        <body>
            <form method="get" action="#"  >
                <input type="file" 
                       id="myfile" 
                       onChange="javascript:showSrc();" 
                       size="30">
                <br>
                <a href="#" id="myframe"></a>
            </form>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题