1

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)
}
3
  • What's the content of storeFileURI? Commented Jun 26, 2016 at 8:03
  • Please share your complete code in JSFiddle or Full StackOverflow code demo. Commented Jun 26, 2016 at 8:06
  • @fracz - look at the 2nd line of the code-snippet. Commented Jun 26, 2016 at 8:16

3 Answers 3

2

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.