I have some code that I think is not working properly now because I added something to get the MIME Type (the real MIME type) using some JS. It calls checkDicomMime(file), and it asynchronously reads the files to be uploaded, and determines if the MIME type matches what I am looking for.
I think the MIME type detection is working, but because it takes time to read the files, I think the rest of the code executes before it is done reading the MIME types.
Previously, I was just checking for a file extension, and that was done synchronously so that the variables in the "reader.onload = function (evt) {" block in the function were set inline. Now, it calls the function, and the function correctly detects the MIME type, but it looks like the calling function completes and the rest of the code executes before the MIME TYPE detection completes, so it POSTS the form for each file in the list before the MIME TYPE detection is done. The total = counts.process is now zero rather than the total number of files to process, so counts and files.process and badfiles are either not getting altered, or they change only after all of the files have been posted. I checked with some debugging and it looks like they are set after the files are sent. Also, that other SO post talks about reading in just the requisite number of bytes to detect the MIME type rather than reading the whole file. Not sure exactly how to do that.
I got the DICOM checking function here: Check Dicom
And there are some discussions about MIME type detection in general using JS here:
How to check file MIME type with javascript before upload?
Relevant code is:
var counts;
// Detects when a Folder is selected, Folder, not a file.
picker.addEventListener('change', e => {
counts = {process:0,omit:0};
requestcounter = 0;
responsecounter = 0;
total = 0;
skipotherrequests = 0;
parsedepoch = new Date().toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/);
datetimestamp = parsedepoch[1] + "-" + parsedepoch[2].replace(/:/g, "-");
//alert(datetimestamp);
picker.setAttribute('data-timestamp', datetimestamp);
// preprocess checking
var badfiles = [];
var filelist = Array.from(picker.files);
filelist.forEach(function(file, index) {
// add it to the list, otherwise skip it
checkDicomMime(file); // calls the check for MIME type.
});
filelist.sort(function(a,b) {
return a.name > b.name;
});
total = counts.process; // omitting the ones that do not pass verification.
badlist = "";
badfiles.forEach( element => badlist += '<div>' + element + '</div>' );
for (var i = 0; i < filelist.length; i++) {
var file = filelist[i];
if (file.process == 0) {
let lineitem = statusitem(file, "Skipping file: " + file.name);
listing.insertAdjacentHTML('beforeend', lineitem);
}
else {
sendFile(file); // sends form and file
}
}
});
function checkDicomMime(file) {
var reader = new FileReader();
reader.readAsArrayBuffer(file);
//Fired after sucessful file read, Please read documenation for FileReader
reader.onload = function (evt) {
if (evt.target.readyState === FileReader.DONE) {
var array = new Uint8Array(evt.target.result);
var s = "";
var start = 128, end = 132;
for (var i = start; i < end; ++i) {
s += String.fromCharCode(array[i]);
}
if (s == "DICM") {
alert("DICM a valid dicom file");
file.process = 1;
counts.process++;
}
else {
alert("DICM not found");
file.process = 0;
counts.omit++;
badfiles.push (file.name);
}
}
}
}
The beginning of then sendFile function is:
sendFile = function(file) {
if (skipotherrequests == 0) {
var timestamp = picker.dataset.timestamp;
var formData = new FormData();
// Set post variables
requestcounter = requestcounter + 1;
formData.set('timestamp', timestamp); // One object file
formData.set('counter', requestcounter);
formData.set('total', total);
formData.set('type', type);
formData.set('webkitpath', file.webkitRelativePath); // One object file
formData.set('file', file); // One object file
//console.log(file);
var request = new XMLHttpRequest();
request.responseType = 'json';
// HTTP onload handler
request.onload = function() {
if (request.readyState === request.DONE) {