Cloudinary Blog

JavaScript File Upload in Two Simple Steps

JavaScript File Upload in Two Simple Steps

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 JavaScript file upload 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.

Want to Learn More About File Uploads?

Recent Blog Posts

Maya Shavin: How I Built My Website

Besides working as a senior front-end developer at Cloudinary, I'm also a content creator, a blogger, and an open-source developer. Follow me at @mayashavin and on mayashavin.com.

In the beginning, my website, mayashavin.com, was mainly for showcasing the status of my development projects and keeping me organized with my speaking schedule. Initially, I built it with Vue.js, later on switching to Nuxt.js (aka Nuxt) for a higher SEO score, and deployed it with Netlify. After some time, I added a blog section with Netlify CMS as the content management system (CMS). Everything was fine until I added more content and features, which led to a significant decline in the site’s performance. Also, the site design needed a modern look. So, I gave the site a makeover.

Read more
Automation Frees Up PetRescue’s Staff to Help Pets Find Their Forever Homes

As we spend more time at home, many of us are adopting pets for the joy, companionship and a surprising range of health benefits. In Australia, where our nonprofit customer PetRescue is located, there’s a shortage of pets to adopt. Last August, the Guardian reported that dog shelters in Australia emptied and adoption fees for puppies were running as high as $AUS1800.

Read more
Cloudinary and Contentful Make Modern Content Management Easier

I am pleased to share that Cloudinary and Contentful have joined forces to further streamline the creation, processing, and delivery of online content through Cloudinary’s digital asset management (DAM) solution and advanced transformation and delivery capabilities for images and video. What’s more, the partnership delivers a headless approach to DAM. By leveraging APIs for media management tasks, marketers and developers alike benefit from an integrated stack of optimized assets for optimization and automation. As a result, page loads are fast and beautiful, and at scale—with less overhead and effort.

Read more
Introducing Cloudinary's Nuxt Module

Since its initial release in October 2016 by the Chopin brothers as a server-side framework that runs on top of Vue.js, Nuxt (aka Nuxt.js) has gained prominence in both intuitiveness and performance. The framework offers numerous built-in features based on a modular architecture, bringing ease and simplicity to web development. Not surprisingly, Nuxt.js has seen remarkable growth in adoption by the developer community along with accolades galore. At this writing, Nuxt has earned over 30K stars on GitHub and 96 active modules with over a million downloads per month. And the upward trend is ongoing.

Read more
How Quality and Quantity can go Hand in Hand

When it comes to quality versus quantity, you’ll often hear people say, “It’s the quality that counts, not the quantity”. While that’s true in many situations, there are also cases where you want both quality and quantity. You may have thousands of images on your website and you want them all to look great. This is especially important if your website allows users to upload their own content, for example, to sell their own products or services. You don't want their poor quality images to reflect badly on your brand.

Read more