<!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>
<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>