QuickBase API Cookbook v3 - Recipe #55
Download file attachments from a table to a local hard drive
download file

require 'quickbase_client'

def downloadFiles1(username,password,cwd,realm)
  qbc = QuickBase::Client.init({"username"=>username,"password"=>password,"org"=>realm})
  #qbc.printRequestsAndResponses=true
  dbid = qbc.findDBByName(cwd)
  qbc.getSchema(dbid)
  dbid = qbc.lookupChdbid(cwd.dup)
  downloadFiles3(qbc,username,password,dbid,"File Attachment")
end

def downloadFiles2(username,password,dbid,fieldName,realm,qid)
  qbc = QuickBase::Client.init({"username"=>username,"password"=>password,"org"=>realm})
  #qbc.printRequestsAndResponses=true
  qbc.getSchema(dbid)
  downloadFiles3(qbc,username,password,dbid,fieldName,realm,qid)
end

def downloadFiles3(qbc,username,password,dbid,fieldName,realm,qid)

   fieldNames = qbc.getFieldNames(dbid)
   clist = ""
   fid = ""
   fieldNames.each{|f|
      id = qbc.lookupFieldIDByName(f)
      fid = id.dup if f == fieldName
      clist << id
      clist << "."
   }
   clist[-1,1]=""

   if fid == ""
     fid = fieldName.dup
     fieldName = qbc.lookupFieldNameFromID(fid)
   end

   qbc.iterateRecords(dbid,fieldNames,nil,qid,nil,clist,"3"){|r|
      if r and r[fieldName] and r[fieldName].length > 0 
         next if r[fieldName].include?("https:") or r[fieldName].include?("http:")
         puts "Downloading #{r[fieldName]}"
         qbc.downLoadFile(dbid,r["Record ID#"],fid)
         if qbc.fileContents
            filename = "#{r["Record ID#"]}_#{r[fieldName]}"
            File.open( filename, "wb" ){|f|
               f.write(qbc.fileContents)
            }
         end   
      end
   }
  puts "\nFinished downloading files in QuickBase Record ID# order."
  puts "(Later copies of files overwrite previous copies)."
  puts "\nYou can repeat this download using downloadFilesToFolder.exe #{username} #{password} #{dbid} #{fid} #{realm} #{qid}."
end

def getInputAndRun
   mycwd = Dir.pwd.gsub('/','\\')

   if ARGV[5]
     downloadFiles2(ARGV[0],ARGV[1],ARGV[2],ARGV[3],ARGV[4],ARGV[5])
   else
   
     print "\nPlease enter your QuickBase username: "
     username = gets.chomp
     print "\nPlease enter your QuickBase password: "
     password = gets.chomp
     print "\nPlease enter your QuickBase realm (default is www): "
     realm = gets.chomp
     realm = "www" unless realm and realm.length > 0
     print "\nEnter 'y' to download the files from a '#{mycwd}' QuickBase application: "
     y = gets.chomp
     
     if y == "y" or y == "Y"
       downloadFiles1(username,password,mycwd,realm)
     else
       print "\nPlease enter the id of your QuickBase table: "
       dbid = gets.chomp
       print "\nPlease enter the name or id of the QuickBase File Attachment field in your table. (e.g. File): "
       fieldName = gets.chomp
       print "\nPlease enter the id of a QuickBase Report to use from your table. (default is 1): "
       qid = gets.chomp
       qid = "1" unless qid and qid.length > 0
       downloadFiles2(username,password,dbid,fieldName,realm,qid)
     end
   end

end

getInputAndRun
This script looks for a QuickBase table with the same name as your current working directory and downloads all the file attachments to your current working directory.  The script starts with older records first, so newer copies of files will normally overwrite older copies.

You can also download the files from any table and file attachment field if you know the table id and the file attachment field id.
https://www.quickbase.com/db/bcdcajmrg?a=dr&r=bx&rl=un3
downloadFilesToFolder.rb
One-Click Ruby Installer for Windows Ruby wrapper for QuickBase HTTP API
Created on Nov. 26, 2007 at  4:23 PM (PST). Last updated by Quick Base on May 16, 2011 at  1:41 PM (PDT). Owned by Quick Base.
Quick Base
Quick Base
Show fields from Show fields from Show fields from a related table
Report Name *
Description
Reports and Charts Panel
Each table has a panel listing its reports and charts, organized in groups.
Please wait while your new report is saved...
Field label
Column heading override
Justification
What does auto mean?
Fields in:

Fields to Extract:

Name for the new table:
Items in the new table are called:

When you bring additional fields into a conversion, Quickbase often finds inconsistencies. For example, say you're converting your Companies column into its own table. One company, Acme Corporation, has offices in New York, Dallas and Portland. So, when you add the City column to the conversion, Quickbase finds three different locations for Acme. A single value in the column you're converting can only match one value in any additional field. Quickbase needs you to clean up the extra cities before it can create your new table. To do so, you have one of two choices:

  • If you want to create three separate Acme records (Acme-New York, Acme-Dallas and Acme-Portland) click the Conform link at the top of the column.
  • If the dissimilar entries are mistakes (say Acme only has one office in New York and the other locations are data-entry errors) go back into your table and correct the inconsistencies—in this case, changing all locations to New York. Then try the conversion again.

Read more about converting a column into a table.

We're glad you're interested in doing more with Quickbase!

Now we need to make you official before you share apps or manage your account.

Verifying your email lets you share Quickbase with others in your company.

Your work email
Your company