javascript - 如何使用户能够使用 vue 从剪贴板粘贴图像?
问题描述
在我的模板中,我有:
<textarea @paste.prevent="fileChange" ref="canvas" /></textarea>
在我的脚本中,我有一个方法:
fileChange(event){
console.log(event.clipboardData.items[0].kind)
....
}
在我的控制台上,当触发 fileChange 事件(通过粘贴)时,我不断收到一个字符串。
解决方案
尝试这个
在我的组件中我使用了这个(listeningn paste 事件)
<b-form-textarea
@paste="pasteFunction"
></b-form-textarea>
在我的函数中得到了这个
pasteFunction(pasteEvent, callback){
if(pasteEvent.clipboardData == false){
if(typeof(callback) == "function"){
console.log('Undefined ')
callback(undefined);
}
};
var items = pasteEvent.clipboardData.items;
if(items == undefined){
if(typeof(callback) == "function"){
callback(undefined);
console.log('Undefined 2')
}
};
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") == -1) continue;
var blob = items[i].getAsFile();
this.addImage(blob)
}
}
最后,我用另一种方法处理图像
addImage(file){
if (!file.type.match('image.*')) {
return new Promise((reject) => {
const error = {
message: 'Solo puede arrastrar imágenes.',
response: {
status: 200
}
}
this.$obtenerError(error)
reject()
return;
})
}
this.files.push(file);
const img = new Image(),
reader = new FileReader();
reader.onload = (e) => this.images.push(e.target.result);
const str = JSON.stringify(file);
reader.readAsDataURL(file);
}
在我的前面可以看到所有这样的图像
<div class="img-wrapper" v-for="(image, index) in dimg" :key="index">
<img style="max-width:230px; max-height: 230px;" thumbnail center rounded class="max-image-upload" :src="image" :alt="`Image Uplaoder ${index}`">
</div>
推荐阅读
- python - 如何在从 Atom 运行的本地命令提示符中查看所有命令历史记录?
- python - Altair 情节在顶部被切断
- javascript - 创建/更新数组数据到 JSON 文件
- ios - 在 Swift iOS 中使用 UITextField 集合时识别 UITextField
- android - Kotlin协程Job的join方法
- javascript - 将两个数组拆分为子数组,然后将相似索引处的子数组连接在一起
- javascript - PHP - 从用于异步 JavaScript 请求的页面重定向用户
- python - Tensorflow 2 kernel_regularizer 在桌面上出现语法错误,但在 Google Colabo 中运行良好
- angular - 无法在 Angular 组件中正确使用 fontawesome 图标
- python - 在 Pandas DataFrame 中将字符串列直接转换为日期格式(不是日期时间)