猿问

怎么用js判断网络图片的后缀?

由于有些网络图片是没有后缀的,所以不能通过后缀的形式来判断,比如
http://daaa2a59-b91e-47ad-a059-a87a8919feb4.coding.io/placeholder.php?size=200x200&bg=dcdcdc&fg=333

不知道怎么通过 js 来获取这张网络图片的 mime?


临摹微笑
浏览 556回答 1
1回答

慕桂英4014372

不跨域或者可以跨域的话function getMime(dist) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Promise(function(resolve,reject) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xhr = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.open('GET', dist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.responseType = 'blob';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.onreadystatechange = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(xhr.readyState == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(xhr.response.type);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.send();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; getMime().then(function(type){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(type)&nbsp; &nbsp; &nbsp; &nbsp; },function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('rejct')&nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答