How to Get the File Name from a Full Path Using JavaScript

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

var filename = fullPath.replace(/^.*[\\\/]/, '')

This will handle both \ OR / in paths

Get filename from string path in javascript?

Try this:

   var nameString = "/app/base/controllers/filename.js";
var filename = nameString.split("/").pop();

Get file name from absolute path in Nodejs?

Use the basename method of the path module:

path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'

Here is the documentation the above example is taken from.

Getting just the filename from a path with JavaScript

var fileNameIndex = yourstring.lastIndexOf("/") + 1;
var filename = yourstring.substr(fileNameIndex);

Get absolute file name of the file selected in HTML input instead of path

You can access the filename from the files attribute of the <input type="file"/> (it is a FileList containing all added files). files[0] will give you the first file as a File object. file.name will get you the filename.

var input = document.getElementById('file');var label = document.getElementById('file-text');
input.addEventListener('change', function() { if(input.files.length > 0) label.textContent = input.files[0].name; else label.textContent = 'Select a file';});
<label for="file" style="cursor: pointer">  <img src="plus.png"><span id="file-text">Select a file</span></label>
<input type="file" id="file" style="display: none"/>

js function to get filename from url

var filename = url.split('/').pop()

I Want to display input file name not full path

A escaped backslash make a backslash in a string '\\'

So you will have to use .split("\\") to get the file name.

Try this:

function change() {  let fnVal = document.getElementById('fn').value.split("\\");;  document.getElementById('file-name').innerHTML = fnVal[fnVal.length - 1];}
  <div><input type="file" name="" id="fn" onchange="change();"><span id="file-name">File Name</span></div>

How to get full path of selected file on change of input type=‘file’ using javascript, jquery-ajax?

For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:

$('input[type=file]').change(function () {
console.log(this.files[0].mozFullPath);
});

https://jsfiddle.net/SCK5A/

So don't waste your time.

edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.

Get directory of a file name in Javascript

I don't know if there is any built in functionality for this, but it's pretty straight forward to get the path.

path = path.substring(0,path.lastIndexOf("\\")+1);


Related Topics



Leave a reply



Submit