Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
366b220b
U
uni-app
项目概览
DCloud
/
uni-app
3 个月 前同步成功
通知
725
Star
38705
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
366b220b
编写于
4月 14, 2021
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(h5): getFileInfo
上级
a120a554
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
127 addition
and
75 deletion
+127
-75
packages/uni-api/src/index.ts
packages/uni-api/src/index.ts
+1
-0
packages/uni-api/src/protocols/file/getFileInfo.ts
packages/uni-api/src/protocols/file/getFileInfo.ts
+9
-0
packages/uni-h5/dist/uni-h5.esm.js
packages/uni-h5/dist/uni-h5.esm.js
+92
-75
packages/uni-h5/src/service/api/file/getFileInfo.ts
packages/uni-h5/src/service/api/file/getFileInfo.ts
+24
-0
packages/uni-h5/src/service/api/index.ts
packages/uni-h5/src/service/api/index.ts
+1
-0
未找到文件。
packages/uni-api/src/index.ts
浏览文件 @
366b220b
...
...
@@ -14,6 +14,7 @@ export * from './protocols/base/canIUse'
export
*
from
'
./protocols/device/makePhoneCall
'
export
*
from
'
./protocols/device/setClipboardData
'
export
*
from
'
./protocols/file/getFileInfo
'
export
*
from
'
./protocols/file/openDocument
'
export
*
from
'
./protocols/location/chooseLocation
'
...
...
packages/uni-api/src/protocols/file/getFileInfo.ts
0 → 100644
浏览文件 @
366b220b
export
const
API_GET_FILE_INFO
=
'
getFileInfo
'
export
type
API_TYPE_GET_FILE_INFO
=
typeof
uni
.
getFileInfo
export
const
GetFileInfoProtocol
:
ApiProtocol
<
API_TYPE_GET_FILE_INFO
>
=
{
filePath
:
{
type
:
String
,
required
:
true
,
},
}
packages/uni-h5/dist/uni-h5.esm.js
浏览文件 @
366b220b
...
...
@@ -4551,6 +4551,13 @@ const API_MAKE_PHONE_CALL = "makePhoneCall";
const
MakePhoneCallProtocol
=
{
phoneNumber
:
String
};
const
API_GET_FILE_INFO
=
"
getFileInfo
"
;
const
GetFileInfoProtocol
=
{
filePath
:
{
type
:
String
,
required
:
true
}
};
const
API_OPEN_DOCUMENT
=
"
openDocument
"
;
const
OpenDocumentProtocol
=
{
filePath
:
{
...
...
@@ -10644,6 +10651,89 @@ const getNetworkType = /* @__PURE__ */ defineAsyncApi("getNetworkType", (_args,
}
return
resolve
({
networkType
});
});
const
files
=
{};
function
urlToFile
(
url
,
local
)
{
const
file
=
files
[
url
];
if
(
file
)
{
return
Promise
.
resolve
(
file
);
}
if
(
/^data:
[
a-z-
]
+
\/[
a-z-
]
+;base64,/
.
test
(
url
))
{
return
Promise
.
resolve
(
base64ToFile
(
url
));
}
if
(
local
)
{
return
Promise
.
reject
(
new
Error
(
"
not find
"
));
}
return
new
Promise
((
resolve
,
reject
)
=>
{
const
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"
GET
"
,
url
,
true
);
xhr
.
responseType
=
"
blob
"
;
xhr
.
onload
=
function
()
{
resolve
(
this
.
response
);
};
xhr
.
onerror
=
reject
;
xhr
.
send
();
});
}
function
base64ToFile
(
base64
)
{
const
base64Array
=
base64
.
split
(
"
,
"
);
const
res
=
base64Array
[
0
].
match
(
/:
(
.*
?)
;/
);
const
type
=
res
?
res
[
1
]
:
""
;
const
str
=
atob
(
base64Array
[
1
]);
let
n
=
str
.
length
;
const
array
=
new
Uint8Array
(
n
);
while
(
n
--
)
{
array
[
n
]
=
str
.
charCodeAt
(
n
);
}
return
blobToFile
(
array
,
type
);
}
function
getExtname
(
type
)
{
const
extname
=
type
.
split
(
"
/
"
)[
1
];
return
extname
?
`.
${
extname
}
`
:
""
;
}
function
getFileName
(
url
)
{
url
=
url
.
split
(
"
#
"
)[
0
].
split
(
"
?
"
)[
0
];
const
array
=
url
.
split
(
"
/
"
);
return
array
[
array
.
length
-
1
];
}
function
blobToFile
(
blob
,
type
)
{
let
file
;
if
(
blob
instanceof
File
)
{
file
=
blob
;
}
else
{
type
=
type
||
blob
.
type
||
""
;
const
filename
=
`
${
Date
.
now
()}${
getExtname
(
type
)}
`
;
try
{
file
=
new
File
([
blob
],
filename
,
{
type
});
}
catch
(
error
)
{
blob
=
blob
instanceof
Blob
?
blob
:
new
Blob
([
blob
],
{
type
});
file
=
blob
;
file
.
name
=
file
.
name
||
filename
;
}
}
return
file
;
}
function
fileToUrl
(
file
)
{
for
(
const
key
in
files
)
{
if
(
hasOwn$1
(
files
,
key
))
{
const
oldFile
=
files
[
key
];
if
(
oldFile
===
file
)
{
return
key
;
}
}
}
var
url
=
(
window
.
URL
||
window
.
webkitURL
).
createObjectURL
(
file
);
files
[
url
]
=
file
;
return
url
;
}
const
getFileInfo
=
/* @__PURE__ */
defineAsyncApi
(
API_GET_FILE_INFO
,
({
filePath
},
{
resolve
,
reject
})
=>
{
urlToFile
(
filePath
).
then
((
res
)
=>
{
resolve
({
size
:
res
.
size
});
}).
catch
((
err
)
=>
{
reject
(
String
(
err
));
});
},
GetFileInfoProtocol
);
const
openDocument
=
/* @__PURE__ */
defineAsyncApi
(
API_OPEN_DOCUMENT
,
({
filePath
},
{
resolve
})
=>
{
window
.
open
(
filePath
);
return
resolve
();
...
...
@@ -10784,80 +10874,6 @@ function parseHeaders(headers) {
});
return
headersObject
;
}
const
files
=
{};
function
urlToFile
(
url
,
local
)
{
const
file
=
files
[
url
];
if
(
file
)
{
return
Promise
.
resolve
(
file
);
}
if
(
/^data:
[
a-z-
]
+
\/[
a-z-
]
+;base64,/
.
test
(
url
))
{
return
Promise
.
resolve
(
base64ToFile
(
url
));
}
if
(
local
)
{
return
Promise
.
reject
(
new
Error
(
"
not find
"
));
}
return
new
Promise
((
resolve
,
reject
)
=>
{
const
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"
GET
"
,
url
,
true
);
xhr
.
responseType
=
"
blob
"
;
xhr
.
onload
=
function
()
{
resolve
(
this
.
response
);
};
xhr
.
onerror
=
reject
;
xhr
.
send
();
});
}
function
base64ToFile
(
base64
)
{
const
base64Array
=
base64
.
split
(
"
,
"
);
const
res
=
base64Array
[
0
].
match
(
/:
(
.*
?)
;/
);
const
type
=
res
?
res
[
1
]
:
""
;
const
str
=
atob
(
base64Array
[
1
]);
let
n
=
str
.
length
;
const
array
=
new
Uint8Array
(
n
);
while
(
n
--
)
{
array
[
n
]
=
str
.
charCodeAt
(
n
);
}
return
blobToFile
(
array
,
type
);
}
function
getExtname
(
type
)
{
const
extname
=
type
.
split
(
"
/
"
)[
1
];
return
extname
?
`.
${
extname
}
`
:
""
;
}
function
getFileName
(
url
)
{
url
=
url
.
split
(
"
#
"
)[
0
].
split
(
"
?
"
)[
0
];
const
array
=
url
.
split
(
"
/
"
);
return
array
[
array
.
length
-
1
];
}
function
blobToFile
(
blob
,
type
)
{
let
file
;
if
(
blob
instanceof
File
)
{
file
=
blob
;
}
else
{
type
=
type
||
blob
.
type
||
""
;
const
filename
=
`
${
Date
.
now
()}${
getExtname
(
type
)}
`
;
try
{
file
=
new
File
([
blob
],
filename
,
{
type
});
}
catch
(
error
)
{
blob
=
blob
instanceof
Blob
?
blob
:
new
Blob
([
blob
],
{
type
});
file
=
blob
;
file
.
name
=
file
.
name
||
filename
;
}
}
return
file
;
}
function
fileToUrl
(
file
)
{
for
(
const
key
in
files
)
{
if
(
hasOwn$1
(
files
,
key
))
{
const
oldFile
=
files
[
key
];
if
(
oldFile
===
file
)
{
return
key
;
}
}
}
var
url
=
(
window
.
URL
||
window
.
webkitURL
).
createObjectURL
(
file
);
files
[
url
]
=
file
;
return
url
;
}
class
DownloadTask
{
constructor
(
xhr
)
{
this
.
_callbacks
=
[];
...
...
@@ -11446,6 +11462,7 @@ var api = /* @__PURE__ */ Object.freeze({
onNetworkStatusChange
,
offNetworkStatusChange
,
getNetworkType
,
getFileInfo
,
openDocument
,
getImageInfo
,
request
,
...
...
@@ -12520,4 +12537,4 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
]);
}
_sfc_main
.
render
=
_sfc_render
;
export
{
_sfc_main$1
as
AsyncErrorComponent
,
_sfc_main
as
AsyncLoadingComponent
,
_sfc_main$n
as
Audio
,
index$4
as
Button
,
_sfc_main$m
as
Canvas
,
_sfc_main$l
as
Checkbox
,
_sfc_main$k
as
CheckboxGroup
,
_sfc_main$j
as
Editor
,
index$5
as
Form
,
index$3
as
Icon
,
_sfc_main$h
as
Image
,
_sfc_main$g
as
Input
,
_sfc_main$f
as
Label
,
LayoutComponent
,
_sfc_main$e
as
MovableView
,
_sfc_main$d
as
Navigator
,
index
as
PageComponent
,
_sfc_main$c
as
Progress
,
_sfc_main$b
as
Radio
,
_sfc_main$a
as
RadioGroup
,
_sfc_main$i
as
ResizeSensor
,
_sfc_main$9
as
RichText
,
_sfc_main$8
as
ScrollView
,
_sfc_main$7
as
Slider
,
_sfc_main$6
as
SwiperItem
,
_sfc_main$5
as
Switch
,
index$2
as
Text
,
_sfc_main$4
as
Textarea
,
UniServiceJSBridge$1
as
UniServiceJSBridge
,
UniViewJSBridge$1
as
UniViewJSBridge
,
_sfc_main$3
as
Video
,
index$1
as
View
,
addInterceptor
,
arrayBufferToBase64
,
base64ToArrayBuffer
,
canIUse
,
closeSocket
,
connectSocket
,
createIntersectionObserver
,
createSelectorQuery
,
createVideoContext
,
cssBackdropFilter
,
cssConstant
,
cssEnv
,
cssVar
,
downloadFile
,
getApp$1
as
getApp
,
getCurrentPages$1
as
getCurrentPages
,
getImageInfo
,
getNetworkType
,
getSystemInfo
,
getSystemInfoSync
,
hideLoading
,
hideNavigationBarLoading
,
hideTabBar
,
hideTabBarRedDot
,
hideToast
,
makePhoneCall
,
navigateBack
,
navigateTo
,
offNetworkStatusChange
,
onNetworkStatusChange
,
onSocketClose
,
onSocketError
,
onSocketMessage
,
onSocketOpen
,
onTabBarMidButtonTap
,
openDocument
,
index$6
as
plugin
,
promiseInterceptor
,
reLaunch
,
redirectTo
,
removeInterceptor
,
removeTabBarBadge
,
request
,
sendSocketMessage
,
setNavigationBarColor
,
setNavigationBarTitle
,
setTabBarBadge
,
setTabBarItem
,
setTabBarStyle
,
setupApp
,
setupPage
,
showActionSheet
,
showLoading
,
showModal
,
showNavigationBarLoading
,
showTabBar
,
showTabBarRedDot
,
showToast
,
switchTab
,
uni$1
as
uni
,
uploadFile
,
upx2px
,
useSubscribe
};
export
{
_sfc_main$1
as
AsyncErrorComponent
,
_sfc_main
as
AsyncLoadingComponent
,
_sfc_main$n
as
Audio
,
index$4
as
Button
,
_sfc_main$m
as
Canvas
,
_sfc_main$l
as
Checkbox
,
_sfc_main$k
as
CheckboxGroup
,
_sfc_main$j
as
Editor
,
index$5
as
Form
,
index$3
as
Icon
,
_sfc_main$h
as
Image
,
_sfc_main$g
as
Input
,
_sfc_main$f
as
Label
,
LayoutComponent
,
_sfc_main$e
as
MovableView
,
_sfc_main$d
as
Navigator
,
index
as
PageComponent
,
_sfc_main$c
as
Progress
,
_sfc_main$b
as
Radio
,
_sfc_main$a
as
RadioGroup
,
_sfc_main$i
as
ResizeSensor
,
_sfc_main$9
as
RichText
,
_sfc_main$8
as
ScrollView
,
_sfc_main$7
as
Slider
,
_sfc_main$6
as
SwiperItem
,
_sfc_main$5
as
Switch
,
index$2
as
Text
,
_sfc_main$4
as
Textarea
,
UniServiceJSBridge$1
as
UniServiceJSBridge
,
UniViewJSBridge$1
as
UniViewJSBridge
,
_sfc_main$3
as
Video
,
index$1
as
View
,
addInterceptor
,
arrayBufferToBase64
,
base64ToArrayBuffer
,
canIUse
,
closeSocket
,
connectSocket
,
createIntersectionObserver
,
createSelectorQuery
,
createVideoContext
,
cssBackdropFilter
,
cssConstant
,
cssEnv
,
cssVar
,
downloadFile
,
getApp$1
as
getApp
,
getCurrentPages$1
as
getCurrentPages
,
get
FileInfo
,
get
ImageInfo
,
getNetworkType
,
getSystemInfo
,
getSystemInfoSync
,
hideLoading
,
hideNavigationBarLoading
,
hideTabBar
,
hideTabBarRedDot
,
hideToast
,
makePhoneCall
,
navigateBack
,
navigateTo
,
offNetworkStatusChange
,
onNetworkStatusChange
,
onSocketClose
,
onSocketError
,
onSocketMessage
,
onSocketOpen
,
onTabBarMidButtonTap
,
openDocument
,
index$6
as
plugin
,
promiseInterceptor
,
reLaunch
,
redirectTo
,
removeInterceptor
,
removeTabBarBadge
,
request
,
sendSocketMessage
,
setNavigationBarColor
,
setNavigationBarTitle
,
setTabBarBadge
,
setTabBarItem
,
setTabBarStyle
,
setupApp
,
setupPage
,
showActionSheet
,
showLoading
,
showModal
,
showNavigationBarLoading
,
showTabBar
,
showTabBarRedDot
,
showToast
,
switchTab
,
uni$1
as
uni
,
uploadFile
,
upx2px
,
useSubscribe
};
packages/uni-h5/src/service/api/file/getFileInfo.ts
0 → 100644
浏览文件 @
366b220b
import
{
defineAsyncApi
,
API_GET_FILE_INFO
,
API_TYPE_GET_FILE_INFO
,
GetFileInfoProtocol
,
}
from
'
@dcloudio/uni-api
'
import
{
urlToFile
}
from
'
../../../helpers/file
'
export
const
getFileInfo
=
defineAsyncApi
<
API_TYPE_GET_FILE_INFO
>
(
API_GET_FILE_INFO
,
({
filePath
},
{
resolve
,
reject
})
=>
{
// TODO 计算文件摘要
urlToFile
(
filePath
)
.
then
((
res
)
=>
{
resolve
({
size
:
res
.
size
,
}
as
UniApp
.
GetFileInfoSuccess
)
})
.
catch
((
err
)
=>
{
reject
(
String
(
err
))
})
},
GetFileInfoProtocol
)
packages/uni-h5/src/service/api/index.ts
浏览文件 @
366b220b
...
...
@@ -5,6 +5,7 @@ export * from './device/getSystemInfo'
export
*
from
'
./device/getSystemInfoSync
'
export
*
from
'
./device/network
'
export
*
from
'
./file/getFileInfo
'
export
*
from
'
./file/openDocument
'
export
*
from
'
./media/getImageInfo
'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录