Reputation: 239
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
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
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
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