Forum Discussion

_anomDiebolt_'s avatar
_anomDiebolt_
Qrew Elite
8 years ago

How To Copy File Attachments Between Tables Without Download?

How To Copy File Attachments  Between Tables Without Download?

This script is intended to be used from the console in its current form although it would not take much in the way of changes to use it within a QuickBase page on button click or other event. It is presented in this fashion to allow users to test it on various file types and in different browsers. I have tested it in Chrome on images, Word documents, pdf documents, MP3 files and it works flawlessly.

Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=574

To use the script fill in these six parameters which represent the {dbid, [Record ID#] and field ID} for the source and target of the file you want to copy and paste the entirety of the script into the console (press F12 to expose):
  let dbidSource = "YOUR SURCE DBID";
  let ridSource = "YOUR SOURCE [Record ID#]";
  let fidSource = "YOUR SOURCE FIELD ID";
  let dbidTarget = "YOUR TARGET DBID";
  let ridTarget = "YOUR TARGET [Record ID#]";
  let fidTarget = "YOUR TARGET FIELD ID";
Please let me know of your success and the conditions you used the script under. I would suggest you test the script in an isolated application to not interfere with your production applications and to simplify the test environment in the event there is an error.

The script does not use the QuickBase API so no application token is needed.

The function copyFile() returns a native JavaScript Promise (not a jQuery Promise) which resolves after the final POSTing of the file to the target locaiton. Since some file uploads can take a while you simply have to wait for the process to complete after pasting your script in the console.

Notes

(1) If some of the coding conventions look new to you the script extensively uses new ES6 features including (a) fat arrow (=> ) functions definitions, (b) let variable definitions, (c) backticks for string interpolation.

(2) The script uses native Promises not jQuery promises (no jQuery is used in this demo)

(3) The script uses the Fetch API which is the replacement for XHR. The whole reason this script can transfer binary files is because the Fetch API support sending blobs of binary data.

(4) The script used the FormData API to submit the file to the target location. The FormData API was the topic of one of my proposed Empower 2017 presentations but I was denied so I will give an impromptu presentation in the hallways if requested. I think I am going to get one of those mini hand held projectors:

https://www.webups.com/sites/default/files/aiptek-t20-pocket-cinema-mini-projector-3.jpg

and maybe one of those big popcorn maker carts:

http://www.fridgefanatic.com/nostalgia-electric/popcorn-machines/kpm508-kettle-popper-popcorn-maker-...

(5) A lot of work went into making the script as compact as possible and avoid a lot of ceremony. To make the most compact usage of QuickBase URL methods some parameters were used that you may not be readily familiar with. Ask if you are curious.

(6) This general trend of using advanced scripting technologies with QuickBase is just going to increase over time:
  1. Greater abstractions through libraries and JavaScript language extensions
  2. More powerful client-side capabilities - the old restrictions on what can be done client-side are evaporating.
  3. More compact code and more usage of DSLs (Domain Specific Languages)

21 Replies

  • Hi Dan,

    I was waiting for this solution. Thanks a lot. I'll try this and will let you know if I face any difficulties.

    Just wanted to check you:

    Do you have some solution to download all the attachment from the table? and import the multiple attachments (means not the multiple files in a single field) into the table?

    Thanks,
    Gaurav
  • Well this script could easily evolve into such a capability but it has to be tested first.

    The capabilities of modern JavaScript are nothing short of amazing. What you are asking for is an called an Iterator in ES6 Javascript. If an object is an Iterator you can loop over it with very compact notation and with high levels of abstractions. Almost any process can be made into an Iterator.

    The only issue there might be is that some files such as video files could fail to upload (with a message disconnect by server) and the remaining chain of files to upload would be abandoned using a simple-minded Iterator. Also there could be potential for accidental abuse if performing large scale iterations involving a lot of resources could be achieved with a couple of lines of code. Hence the need for testing. 
    • GauravSharma3's avatar
      GauravSharma3
      Qrew Commander
      Great. It looks amazing. Well As far as I understand It will work fine if we have only documents, not the videos. Right? if yes, please do your testing and let me know If I can help you in some part of testing. So, we can set the solution of my above question.

      Thanks,
      Gaurav
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      It is the size of the file that is the issue not the type of file. I assume at some point it would fail on a pdf document if it was too large.

      GS> if yes, please do your testing and let me know If I can help you in some part of testing

      You appear to be asking me to test it - I already have. Let me know the results of your tests.
  • Dan:  Would it be possible to give an example of what the 6 variable parameters above would really look like on an actual script.  Not sure when brackets or quotes or https are required if required
    • RobertKreibich's avatar
      RobertKreibich
      Qrew Trainee
      I think I got it.  The full URL including https before the ? is in quotes for the dbid
      The rid and fid are numbers and do not need quotes
      It works beautiful!!!
      Thanks
    • RobertKreibich's avatar
      RobertKreibich
      Qrew Trainee
      it worked on one app but i get the console message on another app of "Uncaught SyntaxError: Unexpected token }" and both apps have the same token properties
    • RobertKreibich's avatar
      RobertKreibich
      Qrew Trainee
      I found the problem.  There are some extra brackets at the end of your Pasties #574  "})();"  The only error I now get is "400 (Bad Request)" but it makes no diff in the correct execution and "done" appears at the bottom  Thanks
  • Hi Dan,

    I know its been a while since this topic had come up. Always fascinated by your solutions like IOL method and so on. Is there anyway we could implement this within QuickBase to work with a button action? For example, if the user clicks on the button, the attachment will be copied over to the attachment field of the target record? I'm very new to JS and struggling to find any solution myself.

    Always appreciate what you offer to the community!


    ------------------------------
    Sini Wickramasinghe
    ------------------------------
    • MarkShnier__You's avatar
      MarkShnier__You
      Icon for Qrew Legend rankQrew Legend
      Alas, Dan has not been seen in on this  Forum for about a year.  Quite a loss.  There is a non code technique to give the appearance of a file attachment in another table where the user can see the file and download it, if that is of interest to you.

      ------------------------------
      Mark Shnier (YQC)
      Quick Base Solution Provider
      Your Quick Base Coach
      http://QuickBaseCoach.com
      markshnier2@gmail.com
      ------------------------------
      • DonLarson's avatar
        DonLarson
        Qrew Commander
        Mark,

        Any issues?  Is he merely retired?

        Don

        ------------------------------
        Don Larson
        Paasporter
        Westlake OH
        ------------------------------
    • HectorAubert's avatar
      HectorAubert
      Qrew Trainee
      Has anyone been able to implement this within QuickBase to work with a button action?

      ------------------------------
      Hector Aubert
      ------------------------------
      • MarkShnier__You's avatar
        MarkShnier__You
        Icon for Qrew Legend rankQrew Legend
        Outfits like Juiced could be hired to do this, but just to make you aware that it's easy to make a link which behaves like a file down load and which can be referenced by lookups or just a formula field.  So the actually file is not loaded twice, but you have a link which behaves as a file attachment field for viewing.

        ------------------------------
        Mark Shnier (YQC)
        Quick Base Solution Provider
        Your Quick Base Coach
        http://QuickBaseCoach.com
        mark.shnier@gmail.com
        ------------------------------