简体   繁体   中英

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)
}

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> 

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

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

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM