[英]Copy and paste images from clipboard, blob Error
I'm trying to copy and paste images from a clipboard, but I get this error message in chrome. 我正在尝试从剪贴板复制和粘贴图像,但是我在chrome中收到此错误消息。 What is wrong?
怎么了?
Error 错误
Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': The argument is not a Blob.
JS JS
$(function() {
$("body").bind("paste", function(ev) {
var $this = $(this);
var original = ev.originalEvent;
var file = original.clipboardData.items[0].getAsFile();
var reader = new FileReader();
reader.onload = function (evt) {
var result = evt.target.result;
var arr = result.split(",");
var data = arr[1]; // raw base64
var contentType = arr[0].split(";")[0].split(":")[1];
// this needs to post to a server route that can accept raw base64 content and save to a file
$.post("/echo/html/", {
contentType: contentType,
data: data
});
$this.append("<img src='" + result + "' />");
};
reader.readAsDataURL(file);
});
});
You're problem is that, when the browser is trying to load the file through the clipboard, it is looking for a blob type of data. 您的问题在于,当浏览器尝试通过剪贴板加载文件时,它正在寻找Blob类型的数据。
Blob, or B inary L arge OB ject is a lengthy string that is your object, but instead of being a file, it is just a string. 斑点,或B inary 大号 ARGE OB JECT是一个漫长的字符串,它是你的目标,但不是作为一个文件,它只是一个字符串。
By trying to copy from the clipboard, which has a copy of the directory of the file, at least from how the error is and how your code is, that's what it looks like, it isn't a blob file type, so Chrome isn't set up to handle the actual file. 通过尝试从具有文件目录副本的剪贴板中进行复制,至少从错误的方式以及代码的方式进行复制,这就是它的样子,它不是blob文件类型,因此Chrome尚未设置为处理实际文件。
I don't know what this is for, but if you are trying to save the files on a database, then what you should do is have the image uploaded through the form, have them placed in a directory with a unique name, and store the location of each image, the actual path, on your database. 我不知道这是干什么的,但是如果您尝试将文件保存在数据库中,那么您应该做的是通过表单上传图像,将它们放置在具有唯一名称的目录中,然后存储数据库中每个图像的位置,即实际路径。
Then, you can loop through it and do what you need to do. 然后,您可以遍历它并执行所需的操作。
If you want, I post some code I have from another project where I did this, so just ask if you want it. 如果您愿意,我会发布我在另一个项目中的代码,因此请问您是否想要。
It will only work if you're using a MySQL server tho; 它仅在使用MySQL服务器时才有效。 the code is written in PHP.
该代码是用PHP编写的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.