Does the user open an already made record and then try and upload something over another attachment that is already there in the field every single time?
I see the issue you are talking about. It seems to me that when you load the form and before you choose a file to be uploaded, the file attachment field has a different set of data than after you choose a file to upload. At first it contains the data that was already in there when you opened the record. After you choose a file and even before you hit save, querying the same field will instead give you data based on what you are about to upload. But annoyingly it does not seem to give you the file size of the file that is already uploaded.
I found a way but it feels extremely backwards and I think it could probably be done in a better way...
Here is another code snippet from Dan. This one will pull the current files size out for you. I have investigated other ways to do this and I am not sure how else but maybe someone else can help.
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&r=kp&rl=cw7That works but you will be doing a promise and an ajax call from every single record when it loads.
Set your URL up correctly and add to this code below and then use the rest of this code and it will print the current file size to your console.
var promise = $.ajax({
url: url,
type: "HEAD"
});
$.when(promise).then(function() {
var size = promise.getResponseHeader("Content-Length");
console.log(size)
});
You would need to use some combination of the first code that was posted with the above code. It would need to grab the current file size from the record on form load before anything else happens. Then it would watch for changes to the field you set and when a change happens it could grab that file size and compare it to the one you pulled on form load. JavaScript isn't a specialty of mine but you will also need to make sure to keep everything within the same scope, or possibly make your first size variable into a global but that isn't great practice usually.
$("#_fid_31").on("change", function(e) {
var data = e.originalEvent.target.files[0];
if (data.size < uploadingFile.size) {
alert('Your file size is larger than the currently uploaded file. Please select a smaller file.');
this.value = "";
else {
DoYourElseStuffHereIfNeeded();
}
}
});
Let me know if I can clarify anything here. I know it isn't a full answer but it is a lot of the needed pieces.