Base64 representing PDF to blob - JavaScript

后端 未结 2 468
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 15:10

I have a Base64 string representing a PDF file. I want to convert it to a file with the Blob object using javascript. After it\'s done i want to save the blob as a PDF file

2条回答
  •  情歌与酒
    2020-12-25 15:20

    This javascript converts a base64 string to a blob object:

    // base64 string
    var base64str = result.pdf;
    
    // decode base64 string, remove space for IE compatibility
    var binary = atob(base64str.replace(/\s/g, ''));
    var len = binary.length;
    var buffer = new ArrayBuffer(len);
    var view = new Uint8Array(buffer);
    for (var i = 0; i < len; i++) {
        view[i] = binary.charCodeAt(i);
    }
    
    // create the blob object with content-type "application/pdf"               
    var blob = new Blob( [view], { type: "application/pdf" });
    var url = URL.createObjectURL(blob);
    

提交回复
热议问题