Upload and Manage Files on Cloudinary with the Filepicker Widget
Tags: webdev, javascript
Published: 2021-12-06 10:42:02

Many internal tools, especially administrative applications, often require file uploading and file management features. File pickers, and file selectors, therefore, are some of the most used components in internal tool development. Developers experience numerous challenges when it comes to implementing these features resulting in delayed operations. This can easily be mitigated by using Appsmith to build and deploy applications quickly. Appsmith has a dedicated [filepicker widget](https://docs.appsmith.com/widget-reference/filepicker) that can be used to upload files to any of the developers' preferred cloud storage services, such as Cloudinary, S3, among others. 

In this short blog, we’re going to focus on one such widget, the filepicker. With this, you can upload files from your local machines to any cloud storage via API. You can upload files by implementing a POST API and reference the base64 or binary version in the post body.

**Features of the Filepicker Widget**

1. The Allowed File Types properties allow us to restrict the types of files that a user (of our application) can upload.

2. The Maximum File Size attribute allows us to specify the total file size that a user can upload.

3. When the user selects files to be uploaded, we can immediately use an API or utilize the S3 plugin to upload the file's base64 to our cloud storage.

4. The visibility property controls the widget's visibility on the page. When the widget is switched off, the widget will not be displayed when the app is published. 

5. There is a parameter that allows us to specify the maximum number of files that a user is allowed to upload.

**How Does the Filepicker Widget Work?**

In this section, we'll look at how the filepicker works. First, let’s set up a new application to get started!



* Create a new account on [Appsmith](https://www.appsmith.com/) (it’s free!), if you are an existing user, log in to your Appsmith account.
* Create a new application by clicking on the Create New button under the Appsmith dashboard.
* We’ll now see a new Appsmith app with an empty canvas and a sidebar with two directories: Datasources and Widgets. This is where we can connect to data sources and find all the widgets.

Now, we’re all set to explore how the filepicker widget works in Appsmith.

Drag and drop the filepicker widget (as shown in the image below). 

![FIlePicker Widget](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe51065790576e7a058_0xc-8l0HUFsmz8lwtS1Wln51SBinUVgQbgeJ2gseWY9e4zGXvc9KdLsmpQR8XAXdrbUMnJfwU6-l06Xt7iPWKMQPJZ5vL8c59PRf5FSywdonQDPFqji-TDrijjbum-Ij8eFkihtm.png)

Now that we have our filepicker widget on the canvas, we can customize the properties as desired. 

After selecting our file(s), we get the following result:

![Appsmith File Widget](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe607405c088996fd37_ZqyNX5FZQqb8mEmchY-KQPAGar6rwIhnPXk_en2-1nWZkWpbbxvrzmpEwQU7LNmdmOSj13qZ2RVZDZ4CQ4kxJ-D9q3ttZMG7Mp1xtUp7zLYDTGJDvjCcjicvtoUiCxU238dEDkjG.png)


**How to manage files on Cloudinary using Appsmith**

**Filepicker configuration**

We'll go over each of the configurations accessible in the Appsmith filepicker one by one.

**Label: **This is used to** **set the label of the Filepicker from the default “Select Files” to any label of our choice.

**Maximum No. of files: **This** **allows us to specify the maximum number of files that a user is authorized to upload.

**Maximum File Size: **This property** **allows us to specify the maximum file size that a user can upload.

**Data Type: **This determines the data format of the files uploaded. 

**Allowed File Types: **Allows us to restrict the types of files that a user can upload. 

Accepts an _array_ of wildcardsimage/*, exact mime types image/jpeg, or file extensions .jpg:['image/*', '.jpg', '.jpeg', '.png', '.gif']

**Required: **When turned on, it marks user input as a required field and disables form submission until the input is made.

**Visible: **Controls widget's visibility on the page. When turned off, the widget will not be visible when the app is published.

See the [documentation](https://docs.appsmith.com/widget-reference/filepicker) to learn more.

**Uploading single and multiple images to cloud storage via Filepicker using a Cloudinary API**

Let's now explore how to upload single or multiple files using filepicker to cloud storage using the Cloudinary API.

**Setting up a data source**

We'll need to connect to our data source by clicking on the Plus icon next to the data sources link in the sidebar, then selecting "Create New" from the menu, as shown below:

![File Widget Source](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe568a52dd37ae63bc3_UM4RzG-m3h8_VsRMWaxt8IKF0yYoIxTDSLRla9pFcR7Oe849Dh1YuB2q3TspgXb19f-AIKYhqwmD-7VY4-8ImG3VnQJ_EylxTpF9apKDhtL6xRPIYHGuHV5xFoy0MmwUvK1t7NzS.png)


After this, we should now choose the "Create New API" option. This will take us to the screen shown below:

![File Widget API](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe5770afef066b997f9_WYqRUeiKtVf4OoNFctRTfYA3r2YSsOFqLlrBuZEI7qERGJE1SJ3TgsLZ1_kCJ2p3RVR1HEe_BgQnGjLtiXk9YbyOipzoH2-5wWCD5zTNofC0PykLykD7qrPqNvZPuz7nAzX8pLT8.png)


We renamed the data source from the default "Api1" to "Cloudinary API" in the image above to have a clear description. 

We will be making use of the Cloudinary API: `[https://api.cloudinary.com/v1_1/{cloud_name}/image/upload](https://api.cloudinary.com/v1_1/olanetsoft/image/upload)` where {cloud_name} is our cloudinary username.

The API requires our Cloudinary `cloud_name` and `upload_preset`.

To get our `cloud_name` and `upload_preset,` we follow the steps listed below. 

The cloud name is obtained from our Cloudinary dashboard, as shown below.

![Cloudinary dashboard](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe559f5d820fc49011b_WfBPGD2Guc1gpqVt68NzkEdz72PCZ9cDYA_fIz1W2jP8_3pkeepvMYsWaDAz9dRsmz4v5aKtfQ32q1ELwOMR4SUwjopJTBc5sIVocmyeRg5IGFHvFtNSFPAtp4Jlbtkz7PiY90nc.png)


An upload preset can be found in the “Upload” tab of our Cloudinary settings page, which we access by clicking on the gear icon in the top right corner of the dashboard page.


![Cloudinary Dashboard](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe56aaefb45bdfb9d65_E05-iCQ_wu6q2F49d9-COtImh0hLARCgNgtscK3BKSS8i2vBxkMhJwbRsyPxOsyso9BfaqU4v7gobHOl2ETOyuL-0OpNeVmJkd5-g7y1rJ5uk3nLkWmfYeGQ-mK_zwgTyS1sVNlX.png)


We then click on the `Upload` tab on the settings page:

![Cloudinary Upload Preset](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe60fffc449a51dc47c_B5nlrtuHobGSvCIlVAIwfymwGQK9P9d1Z_v8HD39pxX1Ty5PzDeL0IclVw4ZO-_P32_6dC2a1coJXTKl_YMmRvY6OEdrKQ6rnBTvjDy1z4pRTet-hqnFvWRx3i47hAIi6oqFxiFE.png)


We scroll down to the bottom of the page to the upload presets section, where we see our upload preset or the option to create one if we don't have any.

Adding the Cloudinary API URL and setting the header should look similar to what we have below:


![API Request Header](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe7160e1244d9354981_TOZV2ZaqZ470Mi_npkv706-EPezkvXBTSwf6DuWYprMh0f7ia3W1Ggv5ffIHNMRaLS1C1IyU_AVyBxf_Ty9LSnNGWQ27BD5qmlC0KueMymjXGjjIVYoplqs-cfiqHXlp_n3o6sm4.png)


Let's go to the "Body" tab and configure it, as shown below.

![API Request Body](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe617fe8975cea6848d_sq4rj5B0N2kMznSTOeoD9Wb1ABoNf1UJ3CHhfTiQ6toTV9jyTsXnCfNFLWFPaaJVG1ZZGIBuRmKEFfPYN_2dBWoZCuginknGbBVgHySF-DCEmblh4DQerS37b-oyDqj-RyuyJBFl.png)


We specified the file(s) to be uploaded as well as our upload preset in the image above, and we configured our request body in a multipart structure.

Now that we've finished configuring the API, let's put it to the test before using it in our filepicker.

We will click on the FIlePicker Widget as shown below and select a file to be uploaded from our system.


![File Picker widget](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe6866732b69746483f_XdxJLIeBxlt1XQhpZGtxqHWLDpAmKvixkJj2HWlva7-Gvi45mvv_GxTT4s6fh01Jb7yc20ygzFCSLeIDlvLzjrTNgOYgO6s8qrNhq7b9Yu9HlMNbgxwSBcuOeC69U9nFIyjLwMYD.png)


Now let’s head over to the API section; the following is the test result:

![API Result](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe64b46caceacf5d4db_Lhe040I7xN1YG30-_xOqMCIm5jHS5ee9gvkbMOMHozxWQ2k-gr1JtL8VjlTeu_U5Sn4MZlb5mnqh375medamHvLDleSnSiPqsRCzAnzzZOWI3N7o73lGEUe4HaY_H9dta022MIWb.png)


Yay! That works! We’ve successfully tested our API and got a valid response.

**Upload images using Filepicker**

Let's upload a single image directly from our filepicker to cloud storage by connecting to the API Datasource we just created. We will head over to the filepicker configuration and update the "onFilesSelected" property.


![File Picker Widget](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe6a6f311289139f72c_bZRG7qdAAqtUDciVOYj9WuuZN5JCOaukw_c8rK3OMPAG3_ar0KBAZAo-uraIAbpTn28lH1b32WJY5KB-ixJWEJICDjA-Za0h9Gow8HI0H7b8dtGdEzbsa4hXaqfkr9pAuz-Mn8T9.png)


As seen below, we can add a success alert to show when the image is successfully uploaded and when there is an issue after selecting our query to be conducted.


![File Picker](https://cdn.hashnode.com/res/hashnode/image/upload/v1638786867274/it7zybQJI.png)


The response below shows a success alert after uploading our image to cloud storage utilizing the Cloudinary API directly from our filepicker widget.


![Result](https://uploads-ssl.webflow.com/61531b23c347e468c0a84236/61a9cbe7f72f5f92fdc81417_JwjVomZOXbv2cGs9J8VNjz7DqMKSP3aZ-Md3NbIBsSKzHYjxoMG7kc2Jq-GR0wv22zallBiL_NVdzxoiKjSK9HGMc87mRD57BBqeq5hH6OzaOTQ3Rpkb7OCq19vD5SV5x8zkvr3u.png)


We hope that you found this short guide helpful. If you want to know how to upload or download files from S3, you can look at [this document](https://docs.appsmith.com/how-to-guides/how-to-upload-to-s3) and [follow this discussion](https://community.appsmith.com/t/how-do-i-upload-multiple-files-to-s3/209). To learn more about how to upload a file to a local server, take a look at [this discussion](https://community.appsmith.com/t/upload-local-file-to-server/290) on our community forum. 

Appsmith has more than 35 [UI widgets](https://www.appsmith.com/widgets), and these can cut your development time in half! To know more, head to our [docs](https://docs.appsmith.com/)! 

Did you get stuck somewhere? We’re happy to help! Join us on [Discord](https://discord.com/invite/rBTTVJp) today! 


---

[Olubisi Idris Ayinde](https://twitter.com/olanetsoft) is a software engineer, technical writer, and community builder. This article was written as part of the Appsmith Writers Program. If you’d like to write for Appsmith, send an email expressing your interest to [writersprogram@appsmith.com](mailto:writersprogram@appsmith.com).