Discussions

Expand all | Collapse all

Formula to capture most recent file size

  • 1.  Formula to capture most recent file size

    Posted 08-24-2020 13:18
    I've inquired before, and am hoping that in the last year someone has figured this out. Is there any way to have a formula field return the current file size in a file attachment field? The end goal is to tell if an image file has been reduced in size or not, which I can do if I can return the file size....

    ------------------------------
    Mike Tamoush
    ------------------------------


  • 2.  RE: Formula to capture most recent file size

    Posted 08-24-2020 15:16
    I see where you had posted about this previously and even see the thread you posted in. https://community.quickbase.com/communities/community-home/digestviewer/viewthread?MessageKey=1ffadf07-9840-4550-a49d-c2b3f1dde0fa&CommunityKey=d860b0f8-6a48-487b-b346-44c47a19a804&tab=digestviewer

    These are in JavaScript because afaik this cannot be done in a native way, I don't think. I see you replied to Dan there, is the issue that you need the script adapted to your exact use case? Have you given it a try at all and do you have anything started that can be worked off of?

    To make the code in the post you replied to work you will need some changes. That specific code works by looking for a change in a field on the form which sounds different than what you want. Can you describe the exact workflow you expect here? To me it sounds like you need this: The record opens and the script runs and pulls the size of the file attachment, It then compares this size to the size in a field called "File Attachment - Old Size" and if the current value is smaller than that value it marks a checkbox saying the record is complete, if there is no older value it puts the current value into the "File Attachment - Old Size" field. Something like that?


  • 3.  RE: Formula to capture most recent file size

    Posted 08-24-2020 15:39
    Thanks Austin. I don't know that I ever realized my old thread had more responses. I will look through it again and try a few things and let you know exactly where I get stuck (if I do). All I really am hoping to do is, someone uploads a photo, script runs and returns the file size and says "if file size > 300kb (I'm making up that number, but Ill figure out what kb) then "check a box'.

    Essentially, I just need to compare the current file size with a static value and return whether it is larger or not (I don't care if it returns the file size and I use a formula to compare, or if it returns a checkmark, or a 'yes' 'no' or what. I just need anything so I can then natively run formulas, etc).

    ------------------------------
    Mike Tamoush
    ------------------------------



  • 4.  RE: Formula to capture most recent file size

    Posted 08-24-2020 15:59
    Austin,

    Now I remember why I couldn't figure things out in the last thread. All the examples don't allow the user to upload the file if it is too large. It gives an alert message. I want to allow the user to upload a larger file. I simply want to know what size the file is (for various reasons this is helpful to me). Bonus points if you can actually reduce the file size to a pre-set width/height on upload, but I suspect that isn't possible.

    ------------------------------
    Mike Tamoush
    ------------------------------



  • 5.  RE: Formula to capture most recent file size

    Posted 08-24-2020 17:38
    Edited by Austin K 08-24-2020 17:50
    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=cw7

    That 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.


  • 6.  RE: Formula to capture most recent file size

    Posted 08-25-2020 11:23
    Thanks Austin. I'll look into all this. This may be a little more advanced than my skill level.

    I do not know what a promise and ajax call is. Is this bad to do on every instance?

    And to your earlier question, 99% of the time the user will be creating a new record and adding a file to a new entry. In rare instances. they will be overwriting an existing file.

    ------------------------------
    Mike Tamoush
    ------------------------------



  • 7.  RE: Formula to capture most recent file size

    Posted 08-25-2020 11:56
    If they are almost always uploading to a blank record then when the file attachment field changes you could store the file size they are uploading and then when they click save(could be your own save button) it could fire off an EditRecord and add that files size to a field. Then when a record that is eventually opened that has a file already uploaded you should have a field already filled out with the previous files size to compare to. This does not help with current file attachments though, which is the bad part.

    Doing a promise isn't bad, a promise is just something in the code that will eventually return a value, it promises to and you wait for it. The "$.when(promise).then(" line is waiting for the promise var to contain something and not be null to proceed in the code. Doing the ajax call on every single link is probably not great though. It is loading the file and then checking the headers to see information about it. So it will need to load every single file to do this which seems wasteful. Quick Base can see the file size of an uploaded file in the file history so it is very frustrating that they do not offer a good way to grab that with an API call, that would fix this easily.


    I think there must be something I am overlooking here. I will try and look into this a bit more later because I would not mind being able to know the file size of a file that is already uploaded too.


  • 8.  RE: Formula to capture most recent file size

    Posted 08-25-2020 14:21
    Yeah for me the most frustrating thing is exactly what you mentioned - I can click on the revision history and see the file size. I know it exists, it torments me I can't just simply grab it somehow....

    ------------------------------
    Mike Tamoush
    ------------------------------