VK Chikkadamalla
VK Chikkadamalla

Reputation: 239

How to get image name from image URl using javascript

I need to display image name in a text field. I got the image url but I am not able to get the image name.

function onSavedDocURISuccesss(imageURI) {
    storeFileURI = imageURI;
    WL.Logger.info("storeFileURI  " + storeFileURI + "  showURIId   "
            + showURIId + "    "
            + storeFileURI.substr(storeFileURI.lastIndexOf('/')))
    if (storeFileURI == null || storeFileURI == undefined)
        storeFileURI = "unsupported file"

    $("#" + showURIId).val(storeFileURI)
}

Upvotes: 1

Views: 2703

Answers (3)

Muhammad Atif Akram
Muhammad Atif Akram

Reputation: 1315

const image_uri = 'https://stackoverflow.com/images/sample.png'
const image_name = image_uri.split('/').pop()
/* alternative method 
 const image_name = image_uri.split('/').reverse()[0]
*/

console.log(image_name)

Upvotes: 0

Roxoradev
Roxoradev

Reputation: 893

You can create a substring of the full path like this:

var fp = "path/to/img.jpg"
$(function(){
  $("#result").text(fp.substring(fp.lastIndexOf("/")+1,fp.length));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p id="result"></p>

Upvotes: 2

David Bradshaw
David Bradshaw

Reputation: 13087

Maybe something like this using split and then taking the last value.

function onSavedDocURISuccesss(imageURI) {
  storeFileURI = imageURI.split('/');
  $("#" + showURIId).val(storeFileURI[storeFileURI.length-1]);
 }

Upvotes: 0

Related Questions