如何使用javascript从图像URl获取图像名称

How to get image name from image URl using javascript

本文关键字:图像 获取 URl javascript 何使用      更新时间:2023-09-26

我需要在文本字段中显示图像名称。我得到了图像的url,但我无法得到图像的名称。

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

您可以像这样创建完整路径的子字符串:

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>

也许像这样使用分割然后取最后一个值。

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)