各種檔案用JS轉Base64之後的data型別

此暱稱已經存在發表於2020-12-24
//txt
data:text/plain;base64,

//doc
data:application/msword;base64,

//docx
data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,

//xls
data:application/vnd.ms-excel;base64,

//xlsx
data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,

//pdf
data:application/pdf;base64,

//pptx
data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,

//ppt
data:application/vnd.ms-powerpoint;base64,

//png
data:image/png;base64,

//jpg
data:image/jpeg;base64,

//gif
data:image/gif;base64,

//svg
data:image/svg+xml;base64,

//ico
data:image/x-icon;base64,

//bmp
data:image/bmp;base64, 
//base64轉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轉file
function blobToFile(theBlob, fileName) {
    theBlob.lastModifiedDate = new Date();
    theBlob.name = fileName;
    return theBlob;
}

 

相關文章