Cloudinary Blog

JavaScript File Upload to Cloudinary

JavaScript File Upload

Uploading software files to the web, mobile, or desktop is as old as the web itself. Billions of files are moved around and downloaded on the Internet daily. Coincidentally, as the de facto language in which many apps are written, JavaScript is also as old as the web.

This article shows you how to enable users to efficiently upload JavaScript files to Cloudinary.

Setup Process

The setup process involves two simple steps: create an HTML form, add JavaScript code to it, and then integrate with Cloudinary’s upload widget. Afterwards, all your users need to do is click the form for a dialog box in which to choose the file for upload.

Creation of HTML Form

Create a file called form.html with the following content:

Copy to clipboard
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JavaScript File Upload</title>
</head>
<body>
      <button id="upload_widget">Upload files</button>
</body>
</html>

Add JavaScript

Next, add the following JavaScript code to form.html:

Copy to clipboard
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JavaScript File Upload</title>
</head>
<body>
      <button id="upload_widget" class="cloudinary-button">Upload files</button>

     <script src="https://widget.cloudinary.com/v2.0/global/all.js" type="text/javascript"></script>
     <script type="text/javascript">  
  var widget = cloudinary.applyUploadWidget({ 
    cloudName: 'demo', 
    uploadPreset: 'blog_upload' }, 
    (error, result) => { 
       if (!error && result && result.event === "success") { 
            console.log('Done uploading..: ', result.info); 
          }

     });    

   document.getElementById("upload_widget").addEventListener("click", function(){
    myWidget.open();
  }, false);
     </script>
</body>
</html>

Traditionally, you would create a form with the <form> element for JavaScript file uploads. By taking advantage of Cloudinary’s upload widget, you can eliminate that step.

Integration With Cloudinary’s Upload Widget

Cloudinary’s upload widget is an API for seamlessly uploading files, such as images, to the cloud. Subsequently, Cloudinary safely stores the files in the cloud with secure backups and revision history.

With Cloudinary’s free-tier plan, you can store up to 75,000 images and videos for a total of 2 GB. You also receive 7,500 transformations and 5 GB net viewing bandwidth per month.

To integrate with the upload widget, do the following:

  1. Sign up for a free Cloudinary account and log in to the dashboard, an example of which looks like this:

    media library

    Displayed are your cloud name, API key, and API secret, which are key credentials for interacting with Cloudinary’s capabilities.

    presets

  2. Create an upload preset with which to centrally define your image-upload options instead of specifying them for each of the uploads.

Note: Be sure to replace the cloudName and uploadPreset values with yours from the Cloudinary dashboard.

You’re now all set. Clicking the Upload files button causes this form widget to pop up:

upload widget

Advantages

The beauty of uploading JavaScript files with the Cloudinary upload widget lies in its versatility. Users can upload from their Instagram, Shutterstock, Google Drive, Dropbox, or Facebook account, or any remote URL from their device.

Upload options

Check out your console after a user upload of, say, an image. You’ll see a returned response that contains all the details of the upload that just occurred, including the image’s URL. The user can store that URL somewhere or assign it to the src attribute of an image tag for display. Talk about convenience and seamlessness exemplified!

More Options to Leverage

Cloudinary’s upload widget offers many more configurations and API methods you can call, e.g., to crop an image before upload. For details, see the related documentation.

Recent Blog Posts

Laravel File Upload to a Local Server Or to the Cloud

Currently, Laravel is the most renowned PHP framework, boasting of a large developer community; several open-source packages, such as Cashier, Sanctum, Scout, and Telescope; and a host of paid platforms, e.g., Laravel Forge, Envoyer, and Vapor. Laravel Forge & Envoyer ably supports deployment and use of Laravel production-based apps.

Read more
TedsVintageArt.com Automates Generation of Captivating Product Images on Cloudinary

At TedsVintageArt.com, we digitally restore historic maps and sell their art prints on multiple platforms, such as Amazon, Etsy, eBay, and Houzz. As our operations ramped up years ago, it took an inordinately long time to generate custom images for our products. Plus, since we sell art, it behooves us to produce multiple high-quality images for each print, which was another time-consuming task.

Read more
lastminute.com Takes a Vacation From Media-Management Headaches With Cloudinary

As a 20-year online leader in travel and leisure retail, Europe-based public company lastminute.com along with its 1,200 employees are committed to enriching the lives of vacationers through comprehensive and enticing products and services. Beyond flight deals and tours, it's the images and videos, whether of that luxury villa in Greece, romantic Parisian street, or exotic stroll through the Marrakech markets, that attract audiences and boost bookings.

Read more