Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中base64的Blob对象_Javascript_Image_Google Chrome_Base64_Blob - Fatal编程技术网

JavaScript中base64的Blob对象

JavaScript中base64的Blob对象,javascript,image,google-chrome,base64,blob,Javascript,Image,Google Chrome,Base64,Blob,我正在尝试实现一个粘贴处理程序,以从用户的剪贴板获取图像。我只想在Google Chrome上运行,我不担心其他浏览器 这是我在互联网上找到的一种方法的一部分,我正在尝试适应它 // Get the items from the clipboard var items = e.clipboardData.items; if (items) { // Loop through all items, looking for any kind of image for

我正在尝试实现一个粘贴处理程序,以从用户的剪贴板获取图像。我只想在Google Chrome上运行,我不担心其他浏览器

这是我在互联网上找到的一种方法的一部分,我正在尝试适应它

// Get the items from the clipboard
var items = e.clipboardData.items;
    if (items) {
    // Loop through all items, looking for any kind of image
        for (var i = 0; i < items.length; i++) {
            if (items[i].type.indexOf("image") !== -1) {
                // We need to represent the image as a file,
                var blob = items[i].getAsFile();
                // and use a URL or webkitURL (whichever is available to the browser)
                // to create a temporary URL to the object
                var URLObj = window.URL || window.webkitURL;
                var source = URLObj.createObjectURL(blob);
                createImage(source);
                }
            }
        }

在第一段代码中,我有一个blob对象表示文件。我尝试了几种方法,但没有得到正确的表示。如何使用它创建base64表示

尼克·雷特拉克在这一页的回答正是我想要的

因此,新的代码是:

var items = e.clipboardData.items;
if (items) {
// Loop through all items, looking for any kind of image
    for (var i = 0; i < items.length; i++) {
        if (items[i].type.indexOf("image") !== -1) {
            // We need to represent the image as a file,
            var blob = items[i].getAsFile();

            var reader = new FileReader();
                reader.onload = function(event){
                    createImage(event.target.result); //event.target.results contains the base64 code to create the image.
                };
                reader.readAsDataURL(blob);//Convert the blob from clipboard to base64
            }
        }
    }
var items=e.clipboardData.items;
若有(项目){
//循环浏览所有项目,查找任何类型的图像
对于(变量i=0;i
jcolebrand我试图使用另一个问题中的base64编码,但我得到了以下错误:异常:TypeError:Object#没有“replace”方法可能blob类型不同,因此我认为我的问题没有重复。。因为其他人的回答在这件事上帮不了我。或者我遗漏了什么……我没有说它们是重复的,我只是把其他人的问题联系起来。主要是为了鼓励讨论。也因为我不知道。哦,好的。谢谢你的帮助和时间,不幸的是,我的问题似乎与你展示的略有不同。你能分享一下你是如何用php处理上传的吗?嗨,我在Java应用程序中处理了这个问题。我必须使用DatatypeConverter.parseBase64Binary(myBase64Code)来获取图像的字节表示。也许PHP中也有类似的东西。
var items = e.clipboardData.items;
if (items) {
// Loop through all items, looking for any kind of image
    for (var i = 0; i < items.length; i++) {
        if (items[i].type.indexOf("image") !== -1) {
            // We need to represent the image as a file,
            var blob = items[i].getAsFile();

            var reader = new FileReader();
                reader.onload = function(event){
                    createImage(event.target.result); //event.target.results contains the base64 code to create the image.
                };
                reader.readAsDataURL(blob);//Convert the blob from clipboard to base64
            }
        }
    }