Skip to content

Commit

Permalink
增加图片上传
Browse files Browse the repository at this point in the history
  • Loading branch information
magic authored and magic committed Sep 10, 2024
1 parent 2c8690a commit 4820b22
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
<head>
<meta charset="UTF-8">
<title>BmobSDK</title>
<style>
img{width: 100px;}
</style>
</head>
<body>
<div id="main">
<input type="file" id="profilePhotoFileUpload" multiple="multiple" >
<div id="img"></div>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion src/lib/config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @FilePath: /hydrogen-js-sdk/src/lib/config.dev.js
*/
// const ROOT = require('../../package.json')
// const HOST = "https://api.bmobcloud.com";
const HOST = "https://api.bmobcloud.com";
// const HOST = 'http://127.0.0.1:8080'
// const HOST = "https://apitest.bmob.cn";
// const HOST = 'http://website-restful.bmobapp.com'
Expand Down
43 changes: 30 additions & 13 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,36 @@ console.log("aaa");

// let BmobSocketIo = Bmob.Socket(33)

// const fileUploadControl = document.getElementById('profilePhotoFileUpload');
// fileUploadControl.onchange = () => {
// const pic = fileUploadControl.files
// let file
// for(let item of pic){
// file = Bmob.File(item.name, item);
// }
// file.save().then(res => {
// const file = res[0]
// console.log(res.length);
// console.log(res,file);
// 获取文件上传控件元素
const fileUploadControl = document.getElementById('profilePhotoFileUpload');

// 当文件选择发生变化时触发该事件处理函数
fileUploadControl.onchange = () => {
// 获取选择的文件列表
const pic = fileUploadControl.files;

// 遍历选择的文件并创建Bmob.File对象
for(let item of pic){
// 初始化Bmob.File,命名文件并关联文件对象
file = Bmob.File(item.name, item);
}

// 保存文件到云端存储服务
file.save().then(res => {
// 保存成功后,获取保存结果
const file = res[0];

// 打印保存文件的数量和文件信息
console.log(res.length);
console.log(res,file,file.url);

// file.url 插入到id为img的层下面最后一个元素图片展示
document.getElementById('img').appendChild(document.createElement('img')).src = file.url;

// 把文件url 文本插入到上一个图片后面
document.getElementById('img').appendChild(document.createTextNode(file.url));


// })
// }
});
}

0 comments on commit 4820b22

Please sign in to comment.