Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
glsx
/
gl_tape
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
3
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 73664db4
authored
Jun 27, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
添加注释
1 parent
098d35a5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
24 deletions
hybrid/html/index.html
hybrid/html/js/OSSUpLoader.js
hybrid/html/index.html
View file @
73664db
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<meta
charset=
"utf-8"
/>
<head>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
>
<meta
charset=
"utf-8"
/>
<title>
file upload oss
</title>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
>
</head>
<title>
file upload oss
</title>
<body>
</head>
<script
src=
"js/uni.webview.1.5.5.js"
></script>
<script
src=
"js/aliyun-oss-sdk-6.18.1.min.js"
></script>
<body>
<script
src=
"js/OSSUpLoader.js"
></script>
<script
src=
"js/uni.webview.1.5.5.js"
></script>
<script>
<script
src=
"js/aliyun-oss-sdk-6.18.1.min.js"
></script>
// document.addEventListener('UniAppJSBridgeReady', function(){
<script
src=
"js/OSSUpLoader.js"
></script>
window
.
msgFromUniapp
=
function
(
accessKeyId
,
accessKeySecret
,
stsToken
,
fileName
,
options
=
{})
{
<script>
if
(
!
accessKeyId
||
!
accessKeySecret
||
!
stsToken
||
!
fileName
)
{
// document.addEventListener('UniAppJSBridgeReady', function(){
throw
new
Error
(
'accessKeyId,accessKeySecret,stsToken,fileName is required'
)
/**
}
* 该函数用于接收来自 UniApp 的消息,它包含了文件上传所需的所有信息
const
opt
=
Object
.
assign
({
accessKeyId
,
accessKeySecret
,
stsToken
},
options
)
* @param {string} accessKeyId - OSS 访问密钥 ID
let
uploader
=
new
OSSUpLoader
(
opt
)
* @param {string} accessKeySecret - OSS 访问密钥 secret
uploader
.
upload
(
fileName
,
function
(
result
)
{
* @param {string} stsToken - 安全令牌服务(STS)令牌,用于临时访问 OSS 存储桶
* @param {string} fileName - 要上传的文件名
* @param {Object} options - (可选)包含上传器配置的对象,如 region
* @throws {Error} 如果缺少必要的参数,将抛出错误
*/
window
.
msgFromUniapp
=
function
(
accessKeyId
,
accessKeySecret
,
stsToken
,
fileName
,
options
=
{})
{
if
(
!
accessKeyId
||
!
accessKeySecret
||
!
stsToken
||
!
fileName
)
{
throw
new
Error
(
'accessKeyId,accessKeySecret,stsToken,fileName is required'
)
}
try
{
// 使用 Object.assign 合并默认配置参数和传入的 options 参数
const
opt
=
Object
.
assign
({
accessKeyId
,
accessKeySecret
,
stsToken
},
options
);
// 创建 OSSUpLoader 实例,传入配置对象
let
uploader
=
new
OSSUpLoader
(
opt
);
// 调用 upload 方法上传文件,并在完成后处理结果
uploader
.
upload
(
fileName
,
function
(
result
)
{
// 通过 uni.postMessage 将上传结果作为数据发送
uni
.
postMessage
({
uni
.
postMessage
({
data
:
{
data
:
{
...
result
,
...
result
,
fileName
fileName
}
}
});
});
uploader
.
destroy
()
// 调用 destroy 方法,这将重置上传器的状态,释放资源
uploader
=
null
uploader
.
destroy
();
})
// 将 uploader 置为 null,释放内存
uploader
=
null
;
});
}
catch
(
e
)
{
// 如果发生错误,将错误信息作为数据发送
uni
.
postMessage
({
data
:
{
code
:
1
,
fileName
,
error
:
e
.
message
}
});
}
}
// }, false);
// }, false);
</script>
</script>
</body>
</body>
</html>
</html>
\ No newline at end of file
\ No newline at end of file
hybrid/html/js/OSSUpLoader.js
View file @
73664db
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment