剪切板获取图片并上传


navigator.permissions.query({ name: "clipboard-read" }).then(result => {
// If permission to read the clipboard is granted or if the user will
// be prompted to allow it, we proceed.
if (result.state == "granted" || result.state == "prompt") {
navigator.clipboard.read().then(data => {
for (const clipboardItem of data) {
let flag = 0;
for (const type of clipboardItem.types) {
clipboardItem.getType(type).then(blob => {
// we can now use blob here
if (blob.type.includes('image')) {
debugger
let name = Math.ceil(Math.random() * 100000).toString();
let file = new window.File([blob], name+'.png', { type: blob.type })
uploadFile(file);
flag++;
}
});
}
if (flag == 0) {
layer.msg('未获取到图片', { icon: 5 })
}
}
});
}
});

 

posted @ 2021-09-13 20:57  a迪  阅读(121)  评论(0编辑  收藏  举报