前端二进制数据互相转换

发布时间:2024年01月17日
/********************** blob ***********************/ 
export const blobToFile = (blob, fileName) => {
    const file = new File([blob], fileName, {type: blob.type});
    return file;
}
export const blobToBase64 = (blob) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const blobToArrayBuffer = (blob) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsArrayBuffer(blob);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const blobToBinary = (blob) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsBinaryString(blob);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const blobToText = (blob) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsText(blob);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const blobToUrl = (blob) => {
    const objectUrl = URL.createObjectURL(blob);
    return objectUrl;
};

/********************** file ***********************/ 
export const fileToBlob = (file) => {
    const blob = new Blob([file], {type: file.type})
    return blob;
};
export const FileToBase64 = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const fileToArrayBuffer = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsArrayBuffer(file);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const fileToBinary = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsBinaryString(file);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};

export const fileToText = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsText(file);
        reader.onload = (e) => { 
            resolve(e.target.result); 
        };
        reader.onerror = () => {
            reject();
        };
    });
};
export const fileToUrl = (file) => {
    const objectUrl = URL.createObjectURL(file);
    return objectUrl;
};
/********************** base64 ***********************/ 
export const base64ToBlob = (code) => {
    const data = code.split(";base64,");
    const contentType = data[0].split(":")[1];
    const binary = window.atob(data[1]);
    const length = binary.length;

    const uInt8Array = new Uint8Array(length);

    for (let i = 0; i < length; ++i) {
        uInt8Array[i] = binary.charCodeAt(i);
    }
    return new Blob([uInt8Array], { type: contentType });
};
/**
 * 
 * @param {*} code base64
 * @param {*} fileName 文件名.后缀
 * @returns 
 */
export const base64ToFile = (code, fileName) => {
    const data = code.split(";base64,");
    const contentType = data[0].split(":")[1];
    const binary = window.atob(data[1]);
    const length = binary.length;

    const uInt8Array = new Uint8Array(length);

    for (let i = 0; i < length; ++i) {
        uInt8Array[i] = binary.charCodeAt(i);
    }
    return new File([uInt8Array], fileName, { type: contentType });
};
export const base64ToArrayBuffer = (code, fileName) => {
    const data = code.split(";base64,");
    const binary = window.atob(data[1]);
    const length = binary.length;

    const uInt8Array = new Uint8Array(length);

    for (let i = 0; i < length; ++i) {
        uInt8Array[i] = binary.charCodeAt(i);
    }
    return uInt8Array
};
export const base64ToBinary = (code) => {
    // 去除base64头
    const data = code.split(";base64,");
    // atob:把 Base64 编码转为原来的字符串或二进制(二进制结果和reader.readAsBinaryString结果一样)
    const binary = window.atob(data[1]);
    return binary
};
/********************** arrayBuffer ***********************/ 
/**
 * 
 * @param {*} buffer 
 * @param {*} type MIME type
 * @returns 
 */
export const arrayBufferToBlob = (buffer, type) => {
    const blob = new Blob([buffer], {type: type});
    return blob;
};
/**
 * 
 * @param {*} buffer 
 * @param {*} fileName 文件名.后缀
 * @param {*} type MIME type
 * @returns 
 */
export const arrayBufferToFile = (buffer, fileName, type) => {
    const file = new File([buffer], fileName, {type: type});
    return file;
};
export const arrayBufferToBase64 = (buffer) => {
    const base64 = btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
    return base64;
};
/********************** binary ***********************/
export const BinaryTobase64 = (binary) => {
    // btoa:字符串或二进制值转为 Base64 编码。
    const base64 = window.btoa(binary);
    return base64
};
/********************** text ***********************/ 

文章来源:https://blog.csdn.net/qq_45313604/article/details/135651613
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。