Base 64 & URL & blob & FileReader & createObjectURL All In One
Base 64 & URL & blob & FileReader & createObjectURL All In One
createObjectURL
/**
* let blob = item.getAsFile();
* let reader = new FileReader();
* event.target.result === reader.result
* base 64
*
*/
// data:image/jpeg;base64,
// data:image/png;base64,
/**
* let blob = item.getAsFile();
* let url = window.URL.createObjectURL(blob);
* blob url
*
*/
// blob:null/2bc6b76b-7293-458d-bc8c-9d242e94a18e
let textarea = document.querySelector(`[data-box="box-textarea"]`);
let box = document.querySelector(`[data-input="text"]`);
box.addEventListener("paste", function(event) {
let items = (event.clipboardData || event.originalEvent.clipboardData).items;
console.log(`clipboardData items: `, JSON.stringify(items, null, 4));
// will give you the mime types
for (const item of items) {
// let item = items[i];
if (item.kind === "file") {
// let blob = item.getAsFile();
// let url = window.URL.createObjectURL(blob);
let blob = item.getAsFile();
let reader = new FileReader();
reader.onload = function(event) {
console.log(`event.target.result =`, event.target.result);
// data:image/png;base64,
let img = document.createElement(`img`);
img.src = event.target.result;
img.setAttribute(`class`, `clearfix`);
// img.class = ".clearfix";
// img.class = "clearfix";
img.style = "width: 30%; height: 30%;";
// img.style = "width: 200px; height: 100px;";
// textarea.appendChild(img);
// textarea.insertAdjacentElement(`beforeend`, img);
textarea.insertAdjacentElement(`afterend`, img);
};
// data url
reader.readAsDataURL(blob);
}
}
});
demos
blob & URL
https://codepen.io/xgqfrms/full/OeVQzY
let log = console.log;
let blob = item.getAsFile();
log(`blob =`, blob);
let url = window.URL.createObjectURL(blob);
log(`url =`, url);
let img = document.createElement(`img`);
// img.src = event.target.result;
img.src = url;
img.setAttribute(`class`, `clearfix`);
img.style = "width: 30%; height: 30%;";
log(`img =`, img);
// textarea.insertAdjacentElement(`afterend`, img);
base 64 & URL
https://codepen.io/xgqfrms/full/EBjQRw
let log = console.log;
let blob = item.getAsFile();
log(`blob =`, blob);
let url = window.URL.createObjectURL(blob);
log(`url =`, url);
let img = document.createElement(`img`);
// img.src = url;
// img.src = event.target.result;
img.src = reader.result;
img.setAttribute(`class`, `clearfix`);
img.style = "width: 30%; height: 30%;";
log(`img =`, img);
// textarea.insertAdjacentElement(`afterend`, img);
js clipboardData
solution
- e.clipboardData.items[0].getAsFile() === blob
- e.clipboardData.items[1].getAsFile() === base 64
// 单聊贴图发送
let box = document.querySelector(`[data-input="text"]`);
box.addEventListener("paste", function (e) {
if (e.clipboardData && e.clipboardData.types) {
if (e.clipboardData.items.length > 0) {
if (/^image\/\w+$/.test(e.clipboardData.items[0].type) || /^image\/\w+$/.test(e.clipboardData.items[1].type)) {
let blob = e.clipboardData.items[0].getAsFile() || e.clipboardData.items[1].getAsFile();
let url = window.URL.createObjectURL(blob);
privewImage(url);
// blob:null/2bc6b76b-7293-458d-bc8c-9d242e94a18e
let uid = conn.getUniqueId();
// 生成本地消息id
let msg = new WebIM.message("img", uid);
// 创建图片 img 消息
msg.set({
apiUrl: WebIM.config.apiURL,
file: {
data: blob,
url: url,
},
to: "test",
// to: "root",
// 接收消息对象
roomType: false,
// 单聊
onFileUploadError(err) {
log("Image Upload Error", err);
},
onFileUploadComplete(data) {
log("Image Upload Complete", data);
},
success(id) {
log("Image Upload Success", id);
alert(`图片发送成功!`);
},
});
conn.send(msg.body);
}
}
}
});
testing
https://codepen.io/xgqfrms/pen/ydNvaY
https://codepen.io/xgqfrms/pen/MMwQOe
demos
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11016097.html
未经授权禁止转载,违者必究!