Base 64 & URL & blob & FileReader & 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

  1. e.clipboardData.items[0].getAsFile() === blob
  2. 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


Base 64 & URL & blob & FileReader & createObjectURL的更多相关文章

  1. base 64 & blob & image url

    base 64 & blob & image url base 64 image & e.clipboardData.items[1] https://codepen.io/x ...

  2. 浅谈 Data URI 与 BASE 64 编码

    前言(废话):鼓捣 Stylish 的时候发现了这么个奇怪的代码行: Data:image/gif;BASE64,R0lGODlhEAAQAKEAAEKF9NPi/AAAAAAAACH5BAEAAAI ...

  3. C# base 64图片编码解码

    使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...

  4. Base 64 编码

    原创地址:http://www.cnblogs.com/jfzhu/p/4020097.html 转载请注明出处 (一)Encoding VS. Encryption 很多人都以为编码(Encodin ...

  5. base 64 编解码器

    base 64 编解码 1. base64的编码都是按字符串长度,以每3个8bit的字符为一组, 2. 然后针对每组.首先获取每一个字符的ASCII编码. 3. 然后将ASCII编码转换成8bit的二 ...

  6. Base 64 & decodeURIComponent

    Base 64 & decodeURIComponent js btoa() & atob() let obj = [{"key":"q",&q ...

  7. 关于BASE 24 ,BASE 64原理以及实现程序

    关于BASE 24 ,BASE 64原理以及实现程序 来源 https://wangye.org/blog/archives/5/ 可能很多人听说过Base64编码,很少有人听说过Base24编码,B ...

  8. 快速掌握 Base 64 | 学 Java 密码系列

    Java 密码系列 - Java 和 JS Base 64 Base 64 不属于密码技术,仅是编码方式.但由于在 Java.JavaScript.区块链等出现的频率较高,故在本系列文章中首先分享 B ...

  9. base 64 bug & encodeURIComponent

    base64 bug & encodeURIComponent window.btoa("jëh²H¶�%28"); // "autoskiptoclMjiu&q ...

随机推荐

  1. https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/util/MurmurHash.html

    https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/util/MurmurHash.html https://github.com/ ...

  2. Web信息收集之搜索引擎-Zoomeye Hacking

    Web信息收集之搜索引擎-Zoomeye Hacking https://www.zoomeye.org ZoomEye(钟馗之眼)是一个面向网络空间的搜索引擎,"国产的Shodan&quo ...

  3. Spring Boot中的静态资源文件

    Spring Boot中的静态资源文件 1.SSM中的配置 2.Spring Boot 中的配置 2.1 整体规划 2.2 源码解读 2.3 自定义配置 2.3.1 application.prope ...

  4. Spring5源码,@ModelAttribute

    一.什么是@ModelAttribute注解 二.@ModelAttribute注解相关代码详解 一.什么是@ModelAttribute注解 @ModelAttribute注解主要用来将请求转换为使 ...

  5. Centos7 安装RabbitMQ 3.6.1

    如果你看过前两章对RabbitMQ已经有了一定了解,现在已经摩拳擦掌,来吧动手吧! 用什么系统 本文使用的是Centos7,为了保证对linux不太熟悉的伙伴也能轻松上手(避免折在安装的路上),下面是 ...

  6. charles(1)解决charles抓包乱码问题

    前言 当使用Charles抓包时,发现数据都是乱码,这时需要安装证书 解决办法 1.点击charles窗口,点击左上角Help-> SSL Proxying -> Install Char ...

  7. HBase原理 – 分布式系统中snapshot是怎么玩的?(转载)

    snapshot(快照)基础原理 snapshot是很多存储系统和数据库系统都支持的功能.一个snapshot是一个全部文件系统.或者某个目录在某一时刻的镜像.实现数据文件镜像最简单粗暴的方式是加锁拷 ...

  8. BSGS及其扩展

    目录 定义 原理 朴素算法 数论分块 例题 Luogu2485 [SDOI2011]计算器 题解 代码 扩展 例题 Luogu4195 [模板]exBSGS/Spoj3105 Mod 代码 之前写了一 ...

  9. 【洛谷 p3383】模板-线性筛素数(数论)

    题目:给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内).(N<=10000000,M<=100000) 解法:1.欧拉筛O(n),数组近乎100KB:2.( ...

  10. STL中去重函数unique

    一:unique(a.begin(),a.end());去重函数只是去掉连续的重复值,对于不连续的值没有影响,SO,在使用前一般需要进行排序处理: 二:  vector<int>::ite ...