[英]How to get image name from image URl using javascript
我需要在文本字段中顯示圖像名稱。 我得到了圖像 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>
也許這樣使用split然后取最后一個值。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.