Tip 27: How to upload files to Dropbox using the Dropbox API in Zoho Creator

Tip 27: How to upload files to Dropbox using the Dropbox API in Zoho Creator

Hi folks, 

It's been a while since our last tip. Hope you're all safe and doing well!

This tip will talk about the Dropbox API that helps you upload files to Dropbox. It's one of the most common actions that you can perform by integrating Zoho Creator and Dropbox. 

To understand how this works, let's create a sample application called File Management App and learn how to upload files to DropBox by adding the Dropbox API to a workflow.

Application flow
  1. The end user will upload a file to the file upload field in a Zoho Creator form.
  2. Once they click the Submit button on the form, the workflow will trigger the Dropbox API and upload the file to your Dropbox.
To do this we need to follow these steps:

  1. Create a form called File Upload in the File Management application, with the following fields:
    1. File Name (Single Line field)
    2. File Upload (File upload field)
  2. Publish the corresponding report to generate the Publish Key.
  3. Create a workflow that uses Dropbox's save URL API to push the uploaded file to Dropbox.
Step 1: Create the File Upload form

First, we need to create the form (as shown in the screenshot below) which we'll use to upload the files into the Creator application.




Step 2: Publish the report

As you're probably aware, for all forms built on Creator, there's an automatic report generated for it. So, for our form, File Upload, a corresponding report has been created. Now, let's publish this report to get the publish key, which will be used to construct the uploaded file's URL.

What is a publish key?

A publish key is a string of characters that Zoho Creator generates to make the report publicly accessible. You'll be able to get the publish key from the permalink of the published report, which is in the below format:

https://creator.zohopublic.com/<workspace_name>/<app_link_name>/report-perma/<report_link_name>/<publish key> 

Step 3: Generate DropBox access token

What is an access token?

Access tokens are used in token-based authentication to allow an application to access an API. In this case, it's a string generated by Dropbox which needs to be included with each subsequent API request to uniquely identify (authenticate) both your app and the end user. 

How to generate an access token for DropBox

You'll need to register your Creator application with Dropbox by creating a new app. That page will guide you through the registration process, specifying the permissions your app needs, and creating an app name.





Now, you can generate an access token for your newly created app by clicking on the Generate button in the OAuth 2
section of your app settings page. 




Step 4: On Success script

Now, let's add a Deluge script to On Success in the form workflow, to push the file that's being uploaded to Zoho Creator to DropBox. We would suggest you use the below syntax on success action on the form. 

    1. //report private key

    2. privatekey = "0v4mapW***************6O2";

    3.  

    4. //DropBox access token

    5.  

    6. dropbox_access_token = "n94**********************pO";

    7.  

    8. // file/folder path in Dropbox where you wish to save the file

    9.  

    10. dropbox_file_path = "/Creator/";

    11.  

    12. //Getting the file name

    13.  

    14. file_name = input.File_Name+input.File_upload.subString(input.File_upload.lastIndexOf("."),input.File_upload.length());

    15.  

    16. //Constructing the file URL

    17.  

    18. fileurl = "https://creatorexport.zoho.com/file" + zoho.appuri + "<report_link_name>/" + input.ID + "/<file_upload_field_link_name>/download/" + privatekey + "?filepath=/" + input.File_upload;

    19.  

    20. url = "https://api.dropboxapi.com/2/files/save_url";

    21.  

    22. headers = "{\"Authorization\":\"Bearer " + dropbox_access_token + "\",\"Content-Type\":\"application/json\"}";

    23.  

    24. data = "{\"path\":\"" + dropbox_file_path +file_name+ "\",\"url\":\"" + fileurl + "\"}";

    25.  

    26. datastring = data.toString();

    27. response = postUrl(url,datastring,headers,false);

The above code will help us upload a file from Zoho Creator to DropBox upon successful form submission in Zoho Creator.

Closing the loop

Note that the postUrl task should complete within 5 minutes, or the operation will time out and the task will fail. Given the likelihood of such scenarios, you shouldn't have any workflows that immediately delete the file or the corresponding record from Creator.

Also, if you want to store files only in Dropbox, we recommend creating a daily or weekly schedule that deletes those records that contain files that were successfully transferred to your Dropbox.

That's an easy tip, isn't it? You should give it a try! And if you run into any difficulties with the process, please comment below and we'll be happy to assist you.






                            Zoho Desk Resources

                            • Desk Community Learning Series


                            • Digest


                            • Functions


                            • Meetups


                            • Kbase


                            • Resources


                            • Glossary


                            • Desk Marketplace


                            • MVP Corner


                            • Word of the Day



                                Zoho Marketing Automation


                                        Manage your brands on social media



                                                Zoho TeamInbox Resources

                                                  Zoho DataPrep Resources



                                                    Zoho CRM Plus Resources

                                                      Zoho Books Resources


                                                        Zoho Subscriptions Resources

                                                          Zoho Projects Resources


                                                            Zoho Sprints Resources


                                                              Qntrl Resources


                                                                Zoho Creator Resources


                                                                  Zoho WorkDrive Resources



                                                                    Zoho Campaigns Resources

                                                                      Zoho CRM Resources

                                                                      • CRM Community Learning Series

                                                                        CRM Community Learning Series


                                                                      • Tips

                                                                        Tips

                                                                      • Functions

                                                                        Functions

                                                                      • Meetups

                                                                        Meetups

                                                                      • Kbase

                                                                        Kbase

                                                                      • Resources

                                                                        Resources

                                                                      • Digest

                                                                        Digest

                                                                      • CRM Marketplace

                                                                        CRM Marketplace

                                                                      • MVP Corner

                                                                        MVP Corner




                                                                            Design. Discuss. Deliver.

                                                                            Create visually engaging stories with Zoho Show.

                                                                            Get Started Now