How To Copy File Attachments Between Tables Without Download?

  • 39
  • 2
  • Question
  • Updated 5 months ago
  • Answered
  • (Edited)
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)
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,884 Points 20k badge 2x thumb

Posted 2 years ago

  • 39
  • 2
Photo of Gaurav Sharma

Gaurav Sharma

  • 5,758 Points 5k badge 2x thumb
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
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,884 Points 20k badge 2x thumb
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. 
Photo of Gaurav Sharma

Gaurav Sharma

  • 5,758 Points 5k badge 2x thumb
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
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,884 Points 20k badge 2x thumb
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.
Photo of Gaurav Sharma

Gaurav Sharma

  • 5,758 Points 5k badge 2x thumb
Ok. I will let you know the results of my testing.
Photo of Robert Kreibich

Robert Kreibich

  • 162 Points 100 badge 2x thumb
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
Photo of Robert Kreibich

Robert Kreibich

  • 162 Points 100 badge 2x thumb
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
Photo of Robert Kreibich

Robert Kreibich

  • 162 Points 100 badge 2x thumb
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
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,694 Points 20k badge 2x thumb
I don't see any problem with the code and I just ran it through a linter to be sure.
Photo of Robert Kreibich

Robert Kreibich

  • 162 Points 100 badge 2x thumb
Dan:  I took some screen crops and sent them to you in an email
Photo of David

David

  • 192 Points 100 badge 2x thumb
Are other people finding this to work?  I get an error when I save.  It says there is a missing parenthesis or that it is an invalid expression. 
(Edited)