Files
TRIMSP/TrimSPweb.js

80 lines
2.2 KiB
JavaScript

// This file contains function that are web app specific
function writeAsciiFile(filename,content) {
// Write string content into ascii file filename
// Go via CGI script
//console.log(filename, content);
// Prepare CGI args
let cgiargs = "?fn="+filename;
let lines = content.split(/\n/);
let prefix = filename.split(/\//);
for (let i=0; i<lines.length; i++) {
cgiargs += "&line" + i + "=" + lines[i];
}
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//console.log("resptext=",xhttp.responseText);
} else if (this.readyState == 4) {
console.log(xhttp.status, xhttp.statusText);
}
}
let request = "/cgi-bin/singleTrimSP.cgi"+cgiargs;
xhttp.open("GET", request, false);
xhttp.send();
// Now you can add a link to the files
let d = document.getElementById("linkDiv");
if (!d) {
d = document.createElement("div");
d.id = "linkDiv";
document.body.appendChild(d);
}
let htmlLink = "Download files: <a href='";
htmlLink += "/tmp/" + prefix[2] + ".tgz'>";
htmlLink += prefix[2] + ".tgz</a>";
d.innerHTML = htmlLink;
return(1);
}
function readAsciiFile(filename) {
// Read ascii file filename and return content in a string
var xhttp;
let file = filename;
if (filename == "" || filename == undefined || filename == null) {
return 0;
} else {
// Make an HTTP request
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 404) {
console.log("File "+file+" not found.");
return(0);
}
}
}
// Force refresh, do not use cache
filename += "?" + Date();
//filename = "http://musruser.psi.ch" + filename;
xhttp.open("GET", filename, false);
xhttp.send();
return(xhttp.responseText);
}
}
function fileExists(filename) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', filename, false);
xhr.send();
if (xhr.status == "404") {
return 0;
} else {
return 1;
}
}