Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.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 如何在java脚本中将PDF文件转换为base64字符串_Javascript_Cordova_Pdf_Extjs_Sencha Touch - Fatal编程技术网

Javascript 如何在java脚本中将PDF文件转换为base64字符串

Javascript 如何在java脚本中将PDF文件转换为base64字符串,javascript,cordova,pdf,extjs,sencha-touch,Javascript,Cordova,Pdf,Extjs,Sencha Touch,我在sencha touch cordova中创建了一个应用程序,在我的应用程序中,我有一个下载PDF的功能 我已经成功下载了一个pdf文件,但现在我想使用JavaScript将pdf转换成base64字符串 有人能告诉我怎么做吗?看看您的JavaScript环境是否有可用的“atob”和“btoa”函数: var encodedData = window.btoa("Hello, world"); // encode a string var decodedData = window.atob

我在sencha touch cordova中创建了一个应用程序,在我的应用程序中,我有一个下载PDF的功能

我已经成功下载了一个pdf文件,但现在我想使用JavaScript将pdf转换成base64字符串


有人能告诉我怎么做吗?

看看您的JavaScript环境是否有可用的“
atob
”和“
btoa
”函数:

var encodedData = window.btoa("Hello, world"); // encode a string
var decodedData = window.atob(encodedData); // decode the string

它们将字符串转换为Base64编码和Base64编码

尝试使用下面的逻辑

 <input id="inputFile" type="file" onchange="convertToBase64();" />

function convertToBase64(){
    //Read File
    var selectedFile = document.getElementById("inputFile").files;
    //Check File is not Empty
    if (selectedFile.length > 0) {
        // Select the very first file from list
        var fileToLoad = selectedFile[0];
        // FileReader function for read the file.
        var fileReader = new FileReader();
        var base64;
        // Onload of file read the file content
        fileReader.onload = function(fileLoadedEvent) {
            base64 = fileLoadedEvent.target.result;
            // Print data in console
            console.log(base64);
        };
        // Convert data to base64
        fileReader.readAsDataURL(fileToLoad);
    }
}

函数convertToBase64(){
//读取文件
var selectedFile=document.getElementById(“inputFile”).files;
//检查文件不为空
如果(selectedFile.length>0){
//从列表中选择第一个文件
var fileToLoad=selectedFile[0];
//用于读取文件的FileReader函数。
var fileReader=newfilereader();
var base64;
//Onload文件读取文件内容
fileReader.onload=函数(fileLoadedEvent){
base64=fileLoadedEvent.target.result;
//在控制台中打印数据
console.log(base64);
};
//将数据转换为base64
readAsDataURL(fileToLoad);
}
}

注意:这段代码取自stackoverflow,但我不记得链接:(

谢谢@David,但它只转换字符串。我需要转换pdf文件。