当前位置:网站首页>微信小程序判断url的文件格式
微信小程序判断url的文件格式
2022-06-29 08:26:00 【swag_特约男演员】
有两种思路
第一种思路
例: http://www.example.com:8888/group1/M00/00/85/rBITVV_Qh2mAdwB0cSE513.jpg
1.查找URL最后一个“.”的索引
2.截取这个索引后面的所有字符(这里就是后缀名)
3.拿到后缀名去匹配判断是否是某某格式
// 文件是否为图片 (方法一)
var isImg = function (url) {
//获取最后一个.的位置
var index = url.lastIndexOf(".")
//获取后缀
var ext = url.substring(index + 1) // wxs不支持substr
// 进行判断
return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].indexOf(ext.toLowerCase()) !== -1
}
第二种思路
1.将URL用“.”分割成数组
2.查找数组最后一项(最后一项就是后缀名)
3.拿到后缀名去判断
// 方法二
var isImg2 = function (url) {
//后缀获取
let suffix = '';
// 获取类型结果
let result = '';
// 分割url的“.”
const flieArr = url.split('.');
// 获取分割后数组最后一项就是后缀
suffix = flieArr[flieArr.length - 1];
if (suffix != "") {
suffix = suffix.toLocaleLowerCase();
// 图片格式
const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
// 进行图片匹配
result = imglist.find(function (item) {
return item === suffix
})
return result
}
}
边栏推荐
猜你喜欢

打印服务IP设置方案

2022第六季完美童模 海口赛区 选拔赛圆满落幕

查找字符串中重复次数最多的元素

手写VirtualDOM

First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8

Tutorial on building open source Internet of things platform

2022第六季完美童模 合肥賽區 决賽圓滿落幕

ActiveMQ message component publish subscribe redelivery message redelivery

ThinkPHP 6 使用 mongoDB

The return values of hostname -f and uname -n may be different
随机推荐
Some behaviors of Apple developers that are prone to investigation
Core development board & debugger
调试H5页面-weinre及spy-debugger真机调试
MySQL的分库分表策略及应用场景
ES6 data type map & set
The sixth season of 2022 perfect children's model Qingyuan competition area audition came to a successful conclusion
Differences between x86 and x64
Mysql使用union all统计多张表组合总数,并分别统计各表数量
io流的总结
积分商城运营要如何做才能获取到利润
乘法器设计(流水线)verilog code
Does the SQL server run with administrator privileges? Or run it as a normal user?
npm常用命令
航芯开发板&调试器
mongoDB 持久化
New paid Tarot calculation source code (with building tutorial)
2022第六季完美童模 合肥赛区 决赛圆满落幕
今天让你知道PMP考试通过率达97%,可信不可信
cokkie和session的区别
Scenario analysis of deadlock during MySQL insert