js & Input & Paste & Clipboard & upload & Image
js & Input & Paste & Clipboard & upload & Image
input paste upload image js
Clipboard_API
https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
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);
}
}
});
type bug
// 单聊贴图发送
let textarea = document.querySelector(`[data-box="box-textarea"]`);
let box = document.querySelector(`[data-input="text"]`);
box.addEventListener("paste", function (e) {
// document.addEventListener("paste", function (e) {
log(`e =`, e);
log(`e.clipboardData`, e.clipboardData);
log(`e.clipboardData.types`, e.clipboardData.types);
log(`e.clipboardData.items`, e.clipboardData.items);
log(`e.clipboardData.items.length`, e.clipboardData.items.length);
log(`e.clipboardData.items[0]`, e.clipboardData.items[0]);
log(`e.clipboardData.items[1]`, e.clipboardData.items[1]);
if (e.clipboardData && e.clipboardData.types) {
if (e.clipboardData.items.length > 0) {
if (/^image\/\w+$/.test(e.clipboardData.items[0].type)) {
let blob = e.clipboardData.items[0].getAsFile();
let url = window.URL.createObjectURL(blob);
// preview
let img = document.createElement(`img`);
// img.src = event.target.result;
img.src = url;
img.setAttribute(`class`, `clearfix`);
img.style = "width: 200px; height: 100px;";
textarea.insertAdjacentElement(`afterend`, img);
// preview
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);
},
});
conn.send(msg.body);
}
}
}
});
OK
// 单聊贴图发送
let textarea = document.querySelector(`[data-box="box-textarea"]`);
let box = document.querySelector(`[data-input="text"]`);
box.addEventListener("paste", function (e) {
// document.addEventListener("paste", function (e) {
log(`e =`, e);
log(`e.clipboardData`, e.clipboardData);
log(`e.clipboardData.types`, e.clipboardData.types);
log(`e.clipboardData.items`, e.clipboardData.items);
log(`e.clipboardData.items.length`, e.clipboardData.items.length);
log(`e.clipboardData.items[0]`, e.clipboardData.items[0]);
log(`e.clipboardData.items[1]`, e.clipboardData.items[1]);
if (e.clipboardData && e.clipboardData.types) {
if (e.clipboardData.items.length > 0) {
// if (/^image\/\w+$/.test(e.clipboardData.items[0].type)) {
// let blob = e.clipboardData.items[0].getAsFile();
if (/^image\/\w+$/.test(e.clipboardData.items[1].type)) {
let blob = e.clipboardData.items[1].getAsFile();
let url = window.URL.createObjectURL(blob);
// preview
let img = document.createElement(`img`);
// img.src = event.target.result;
img.src = url;
img.setAttribute(`class`, `clearfix`);
img.style = "width: 200px; height: 100px;";
textarea.insertAdjacentElement(`afterend`, img);
// preview
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);
},
});
conn.send(msg.body);
}
}
}
});
ClipboardEvent
https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent
clipboardData
https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData
https://caniuse.com/#search=clipboardData
https://caniuse.com/#search=execCommand
https://codepen.io/netsi1964/pen/IoJbg
https://stackoverflow.com/questions/50427513/html-paste-clipboard-image-to-file-input
https://mobiarch.wordpress.com/2013/09/25/upload-image-by-copy-and-paste/
https://stackoverflow.com/questions/490908/paste-an-image-from-clipboard-using-javascript/4400761
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
js & Input & Paste & Clipboard & upload & Image的更多相关文章
- js & input event & input change event
js & input event & input change event vue & search & input change <input @click=& ...
- angular.js input
<!DOCTYPE html> <html ng-app="app"> <head> <meta charset="UTF-8& ...
- js input输入框的总结
一.输入框只能输入数字 原文:https://www.cnblogs.com/sese/p/5872144.html 分享下js限制输入框中只能输入数字的方法,包括整数与小数,分享几个例子,有需要的朋 ...
- js input复选框选中父级同时子级也选中
js实现复选框选中父级元素子级元素也选中,没有子级元素选中父级也不选中的效果 HTML <tr> <td> <label> <input name=" ...
- js input radio点击事件
html代码: <input type="radio" name="myname" value="1" />1 <inpu ...
- JS input文本框禁用右键和复制粘贴功能的代码
代码如下: function click(e) { if (document.all) { ||||) { oncontextmenu='return false'; } } if (document ...
- JS input file 转base64 JS图片预览
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js input输入事件兼容性问题
if(navigator.userAgent.indexOf('Android') > -1){ $("#sign").on("input", funct ...
- JS INPUT输入的时候全角自动转为半角
function CtoH(obj){var str=obj.value;var result="";for (var i = 0; i < str.length; i++) ...
随机推荐
- Centos GitLab 配置
如果重启之后,gitlab-ctl restart报"runsv no running"错,先运行下面命令 systemctl start gitlab-runsvdir.serv ...
- 详解Go中内存分配
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码15.7 介绍 Go 语言的内存分配器就借鉴了 TCMalloc 的 ...
- 用tqdm和rich为固定路径和目标的python算法代码实现进度条
适用场景 在存在固定长度的算法中可以可视化算法执行的过程,比如对一个固定长度的数组的遍历,就是一种适合使用进度条来进行可视化的场景.而一些条件循环,比如while循环,不一定适合使用进度条来对算法执行 ...
- 通过动态构建Expression Select表达式并创建动态类型来控制Property可见性
通过动态构建Expression Select表达式并创建动态类型来控制Property可见性 项目中经常遇到的一个场景,根据当前登录用户权限,仅返回权限内可见的内容.参考了很多开源框架,更多的是在V ...
- html播放音乐目前只支持ie
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java中== equal hashcode 比较的区别
== 基本数据类型是进行数值的比较 引用数据类型比较的是两对象的地址值 实际上都是进行直接值的比较 equal(Object) Object型参数,任何类型的实参都可以传入,只有实参是一个字符串且内容 ...
- CF-1451 E Bitwise Queries 异或 交互题
E - Bitwise Queries 传送门 题意 有一组序列,长度为 \(n(4\le n \le 2^{16})\),且 \(n\) 为 2 的整数次幂,序列中数值范围为 [0,n-1], 每次 ...
- Codeforces Round #626 Div2 D,E
比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...
- cf 1305 E. Kuroni and the Score Distribution
题目传送门:E. Kuroni and the Score Distribution 题目大意:给n和m,输出n个数,这些数里必须要有m对a[i]+a[j]==a[k] ( i < j < ...
- Java 窗口 小马图像窗口
写在前面: eclipse接着爽到 全是借鉴的,东改西改,而且记不住原网址了 两个月前写的,忘了思路,嗯,It just works 运行效果: 图像随便选(放到*jar所在目录*\\pictures ...