Skip to content

Instantly share code, notes, and snippets.

@ajinabraham
Created August 31, 2018 11:21
Show Gist options
  • Save ajinabraham/bf2945741346c2fcdcd1b2e3df8baab0 to your computer and use it in GitHub Desktop.
Save ajinabraham/bf2945741346c2fcdcd1b2e3df8baab0 to your computer and use it in GitHub Desktop.
<input id="file" type="file" name="file" />
<input class="btn btn-success" type="submit" value="Upload" onclick="uploadFile()"></br>
<input type="button" value="Scan File" onclick="scanFile()"></br>
<input type="button" value="Get JSON" onclick="getJSONReport()"></br>
<input type="button" value="Get PDF" onclick="getPDFReport()"></br>
<input type="button" value="Delete Scan" onclick="deleteSCAN()"></br>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
// Set API Key
const api_key = "{{ api }}";
var scan_hash, scan_type, file_name;
var rescan = "0"
var uploadFile = function() {
var data = new FormData();
data.append('file', $('#file')[0].files[0]);
$.ajax({
url: 'http://localhost:8000/api/v1/upload',
method: "POST",
data: data,
dataType: "text",
cache: false,
crossDomain: true,
processData: false,
contentType: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", api_key);
},
success: function(data) {
console.log("Scan");
console.log(data)
alert(data)
var scanobj = JSON.parse(data)
scan_hash = scanobj.hash;
scan_type = scanobj.scan_type;
file_name = scanobj.file_name;
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
alert(textStatus + " " + errorThrown);
}
});
}
var scanFile = function() {
$.ajax({
url: 'http://localhost:8000/api/v1/scan',
method: "POST",
dataType: "text",
crossDomain: true,
data: {
"hash": scan_hash,
"scan_type": scan_type,
"file_name": file_name,
"re_scan": rescan
},
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", api_key);
},
success: function(data) {
console.log("Scan");
console.log(data)
alert(data)
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
alert(textStatus + " " + errorThrown);
}
});
}
var deleteSCAN = function() {
$.ajax({
url: 'http://localhost:8000/api/v1/delete_scan',
method: "POST",
dataType: "text",
crossDomain: true,
data: {
"hash": scan_hash,
},
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", api_key);
},
success: function(data) {
console.log("Delete Scan");
console.log(data)
alert(data)
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
alert(textStatus + " " + errorThrown);
}
});
}
var getJSONReport = function() {
$.ajax({
url: 'http://localhost:8000/api/v1/report_json',
method: "POST",
dataType: "text",
crossDomain: true,
data: {
"hash": scan_hash,
"scan_type": scan_type,
},
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", api_key);
},
success: function(data) {
console.log("JSON Report");
console.log(data)
alert(data)
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
alert(textStatus + " " + errorThrown);
}
});
}
var getPDFReport = function() {
$.ajax({
url: 'http://localhost:8000/api/v1/download_pdf',
method: "POST",
dataType: 'binary',
xhrFields: {
responseType: 'blob',
},
crossDomain: true,
data: {
"hash": scan_hash,
"scan_type": scan_type,
},
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", api_key);
},
success: function(data) {
console.log("PDF Report");
console.log(data);
var blob = new Blob([data], {
type: "application/octet-stream"
});
var fileName = "Report.pdf";
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
alert(textStatus + " " + errorThrown);
}
})
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment