-----------------------------------------------------------------------------------
JAVASCRIPT
var data = "";
function previewFile() {
var preview = document.getElementById('img2');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
console.log(file.type);
console.log(file)
// when user select an image, `reader.readAsDataURL(file)` will be triggered
// reader instance will hold the result (base64) data
// next, event listener will be triggered and we call `reader.result` to get
// the base64 data and replace it with the image tag src attribute
reader.addEventListener("load", function() {
console.log(file.type);
preview.src = reader.result;
//preview.setAttribute('style', 'width:50%;');
//preview.setAttribute('style', 'height:auto;');
console.log('after preview');
//data = reader.readAsDataURL(file);
data = file;
//console.log(reader.result); // this is where the data is
//console.log(data);
}, false);
if (file) {
console.log('inside if');
reader.readAsDataURL(file);
} else {
console.log('inside else');
}
}
var tabledata;
$('.buttonz').on('click', function() {
// Initialize the Amazon Cognito credentials provider
AWS.config.region = 'us-east-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:78382418-c726-45f9-9415-6ad97790d50f',
});
// Change button to loading
var $this = $(this);
$this.text('loading...');
$this.prop("disabled", true);
var params = {
Body:data,
Bucket: "lambda-textract-demo",
Key: "example.pdf",
ContentType: "application/pdf",
ContentEncoding:'base64',
};
var s3 = new AWS.S3();
s3.putObject(params, function(err, data) {
if (err)
console.log(err, err.stack); // an error occurred
else
console.log("success, added file to s3"); // successful response /*
data = {
ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"",
ServerSideEncryption: "AES256",
VersionId: "Ri.vC6qVlA4dEnjgRV4ZHsHoFIjqEMNt"
};
});
setTimeout(function()
{
$this.text('Uploaded to S3...');
$this.prop("disabled", true);
callTextractAPI($this);
}, 2000);
});
function callTextractAPI($this)
{
console.log("Calling Textract API");
$this.text("Calling Textract API...");
$this.prop("disabled", true);
// Prepare to call Lambda function
var lambda = new AWS.Lambda({region: 'us-east-1'});
var pullParams = {
FunctionName : 'lambda-textract-demo-stac-TextractDocumentAnalyzer-60CZETW7NJ7',
InvocationType : 'RequestResponse',
LogType : 'None',
Payload : '{"ExternalBucketName": "lambda-textract-demo","ExternalDocumentPrefix": "example.pdf"}'
};
lambda.invoke(pullParams, function(err, data) {
if (err) {
prompt(err);
} else {
$this.text('Parsing Table & Form results...');
$this.prop("disabled", true);
pullResults = JSON.parse(data.Payload);
//console.log(pullResults);
console.log(pullResults['tables']);
var tablement = document.getElementById("container11");
var selectement = document.getElementById("options");
var numtables = Object.keys(pullResults["tables"]).length;
console.log("Number of tables found = " + numtables);
if(numtables > 0){
tablement.innerHTML = pullResults["tables"]["0"]["html"];
tabledata = pullResults;
// fill out options
var opt;
for(var i=0; i