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

时间:2022-08-23 10:37:36

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

2 个解决方案

#1


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>

#2


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

#1


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>

#2


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