Tuesday, January 21, 2020

read file and show as pdf

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>File Read</title>
</head>

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

    <div id="output"></div>
    <script type="text/javascript">
        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
                    // window.open(base64,"_blank");

                    //-----First Method
                    // const downloadLink = document.createElement("a");
                    // const fileName = "abc.pdf";
                    // downloadLink.href = base64;
                    // downloadLink.download = fileName;
                    // downloadLink.click();

                    //-----Second Method
                    // var obj = document.createElement('object'); //change  obj.src =  base64; to  obj.data =  base64;
                    // var obj = document.createElement('embed');
                    var obj = document.createElement('iframe');
                    obj.style.width = '100%';
                    obj.style.height = '842pt';
                    obj.type = 'application/pdf';
                    obj.src = base64;
                    document.body.appendChild(obj);


                };

                // Convert data to base64
                fileReader.readAsDataURL(fileToLoad);
            }
        }
    </script>

</body>

</html>

No comments:

Post a Comment

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...