my.detectfiletype(object object)
基础库 2.8.8 或更高版本; 且 支付宝客户端 10.3.50 或更高版本; 若版本较低,建议采取 兼容处理 。
支小宝客户端 支持
安诊儿客户端 支持
小程序开发者工具: 支持。3.8.3 及以上版本支持该接口调用
主体: 企业支付宝小程序 、 个人支付宝小程序
相关文档: 本地文件
检测文件类型。
object object
查看示例
| 属性 | 类型 | 默认值 | 必填 | 描述 |
|---|---|---|---|---|
| filepath | string | - | 是 | 文件路径。支持 本地文件 路径 |
| success | function | - | 否 | 调用成功的回调函数 |
| fail | function | - | 否 | 调用失败的回调函数 |
| complete | function | - | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
success 回调函数
参数
object res
查看示例
| 属性 | 类型 | 描述 |
|---|---|---|
| type | string | 文件类型(mime) |
| extension | string | 文件扩展名 |
type 和 extension 枚举
image
| extension | type |
|---|---|
| .jpg | image/jpeg |
| .jp2 | image/jp2 |
| .png | image/png |
| .gif | image/gif |
| .webp | image/webp |
| .cr2 | image/x-canon-cr2 |
| .tif | image/tiff |
| .bmp | image/bmp |
| .jxr | image/vnd.ms-photo |
| .psd | image/vnd.adobe.photoshop |
| .ico | image/vnd.microsoft.icon |
| .dwg | image/vnd.dwg |
| .exr | image/x-exr |
video
| extension | type |
|---|---|
| .mp4 | video/mp4 |
| .m4v | video/x-m4v |
| .mkv | video/x-matroska |
| .webm | video/webm |
| .mov | video/quicktime |
| .avi | video/x-msvideo |
| .wmv | video/x-ms-wmv |
| .mpg | video/mpeg |
| .flv | video/x-flv |
| .3gp | video/3gpp |
audio
| extension | type |
|---|---|
| .mid | audio/midi |
| .mp3 | audio/mpeg |
| .m4a | audio/mp4 |
| .ogg | audio/ogg |
| .flac | audio/x-flac |
| .wav | audio/x-wav |
| .amr | audio/amr |
| .aac | audio/aac |
| .aiff | audio/x-aiff |
archive
epub 文件实际返回 和 zip 文件一致。
| extension | type |
|---|---|
| .epub | application/epub zip |
| .zip | application/zip |
| .tar | application/x-tar |
| .rar | application/vnd.rar |
| .gz | application/gzip |
| .7z | application/x-7z-compressed |
documents
| extension | type |
|---|---|
| application/pdf | |
| .doc | application/msword |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .xls | application/vnd.ms-excel |
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .ppt | application/vnd.ms-powerpoint |
| .pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
其它
| extension | type | 说明 |
|---|---|---|
| '' | empty | 空文件 |
| '' | unknown | 未知文件类型 |
fail 回调的参数为 object,error 属性为错误码,errormessage 属性为错误消息。
| 错误码 | 错误消息 | 凯发app官方网站的解决方案 |
|---|---|---|
| 2 | filepath 格式错误 |
请检入参 filepath 是否正确,参考入参类型和描述 |
| 60001 | filepath 指向的文件不存在 |
请检查文件是否存在,可调用 filesystemmanager.access 判断。 |
| 60002 | filepath 指向目录而非文件 |
请检查入参是否为本地文件路径。 |
my.detectfiletype(object object)
下载文件后获取文件类型
success 参数
fail 参数
my.detectfiletype({
filepath: `${my.env.user_data_path}/test.txt`,
success(res) {
console.log(res);
},
fail(err) {
console.log(err);
}
});
my.downloadfile({
url: 'https://img.alicdn.com/tfs/tb1x669sxxxxxbdafxxxxxxxxxx-520-280.jpg',
success(res) {
console.log(res)
my.detectfiletype({
filepath: res.apfilepath,
success(res) {
console.log(res);
},
fail(err) {
console.log(err);
}
})
}
});
{
"type":"image/png",
"extension":".png"
}
{
"error":"2",
"errormessage":"filepath 格式错误"
}
-
tipepub 符合标准的 zip 格式,入参 filepath 传入 epub 文件时,获取到的文件扩展名为
.zip。 -
tipios 端判断图片类型需选择原图,否则无法正确判断出 gif 格式的图片。