GVKun编程网logo

将Blob转换为base64

28

在本文中,我们将为您详细介绍将Blob转换为base64的相关知识,此外,我们还会提供一些关于AzureFunctionJS:从Blob获取图像并转换为base64、base64和Blob相互转换、b

在本文中,我们将为您详细介绍将Blob转换为base64的相关知识,此外,我们还会提供一些关于Azure Function JS:从Blob获取图像并转换为base64、base64 和 Blob 相互转换、base64转换为图片以及图片转换为base64码、blob转base64位 base64位转blob的有用信息。

本文目录一览:

将Blob转换为base64

将Blob转换为base64

这是我要BlobBase64字符串进行编码的代码片段:

该带注释的部分有效,当由此生成的URL设置为img src时,它将显示图像:

var blob = items[i].getAsFile();//var URLObj = window.URL || window.webkitURL;//var source = URLObj.createObjectURL(blob);//console.log("image source=" + source);var reader = new FileReader();reader.onload = function(event){console.log(event.target.result)}; // data url!var source = reader.readAsBinaryString(blob);

问题在于下面的代码,生成的源变量为null

更新:

有没有更简单的方法可以使用JQuery做到这一点,从而能够从Blob文件中创建Base64字符串,如上面的代码所示?

答案1

小编典典
var reader = new FileReader();     reader.readAsDataURL(blob);      reader.onloadend = function() {         var base64data = reader.result;                         console.log(base64data);     }

将文档 readAsDataURL编码为base64

Azure Function JS:从Blob获取图像并转换为base64

Azure Function JS:从Blob获取图像并转换为base64

我在自己的网站上进行了测试,效果很好。您可以参考以下代码进行故障排除。

在index.js中,注意在参数中添加myinput in

module.exports = async function (context,req,myinput) {
    context.log('JavaScript HTTP trigger function processed a request.');
    let me = context.bindings.myinput;
    console.log(me);
    console.log(me.toString('base64'));
}

本地主机中的快照: enter image description here

门户中的快照: enter image description here

base64 和 Blob 相互转换

base64 和 Blob 相互转换

Base64 to Blob

function dataURLtoBlob(dataurl) {
    var arr = dataurl.split('',''), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}

Blob to Base64

function blobToDataURL(blob, callback) {
    let a = new FileReader();
    a.onload = function (e) { callback(e.target.result); }
    a.readAsDataURL(blob);
}

 

base64转换为图片以及图片转换为base64码

base64转换为图片以及图片转换为base64码

public class base64Change {
    /**
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
// 解密
            byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    //图片转化成base64字符串  
    public static String GetImageStr()  
    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
        String imgFile = "F:\\tupian\\a.jpg";//待处理的图片
                         // 地址也有写成"F:/deskBG/86619-107.jpg"形式的
        InputStream in = null;  
        byte[] data = null;  
        //读取图片字节数组  
        try   
        {  
            in = new FileInputStream(imgFile);          
            data = new byte[in.available()];  
            in.read(data);  
            in.close();  
        }   
        catch (IOException e)   
        {  
            e.printStackTrace();  
        }  
        //对字节数组Base64编码  
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(data);//返回Base64编码过的字节数组字符串  
    }

}
public String ChangeBase64(String base64data,Integer certifiedtype,Integer userid,HttpServletRequest request) {
    //这一步很重要很重要很重要,因为base64的数据会有data:base64img,
    //所有需要将这个截取掉之后转化,不然就是空白的打不开的文件
    String base64img = certifieddata.substring(base64data.indexOf(",")+1);

    FileUploadUtils.generateImage(base64img,"F:/deskBG/86619-107.jpg");
    //return null;是为了测试的,实际需求自己定
    return null;
  }

 

blob转base64位 base64位转blob

blob转base64位 base64位转blob

//**dataURL to blob**
    function dataURLtoBlob(dataurl) {
        var arr = dataurl.split('',''), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], { type: mime });
    }

    //**blob to dataURL**
    function blobToDataURL(blob, callback) {
        var a = new FileReader();
        a.onload = function (e) { callback(e.target.result); }
        a.readAsDataURL(blob);
    }

    //test:
    //var blob = dataURLtoBlob(''data:text/plain;base64,YWFhYWFhYQ=='');
    //blobToDataURL(blob, function (dataurl) {
    //    console.log(dataurl);
    //});

关于将Blob转换为base64的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Azure Function JS:从Blob获取图像并转换为base64、base64 和 Blob 相互转换、base64转换为图片以及图片转换为base64码、blob转base64位 base64位转blob的相关信息,请在本站寻找。

本文标签: