The procedures below describe how to Node.js file upload’s to your local server or to the cloud at Cloudinary.
First, download and install Node.js on your system. Afterwards, set up a Node.js back-end server with a package, such as Express. Do the following:
uploaddirectory and run
npm initthere to generate a
In the same directory, install Express:
npm install express --save
index.jsfile with the code below:
Run the project with the command
Your back-end server is now up and running.
imagesdirectory in which to display the details of the recently uploaded file in your console. See this example:
For simultaneous multiple file uploads, just change
5 above can be any numeric value, denoting the number of files you plan to upload.
Completing step 1 enables you to upload files to your local server. For multiple files, best store them in a central location like Cloudinary and behind a content delivery network (CDN) at scale for efficient retrieval and delivery to users.
This step establishes the mechanics for uploading files to Cloudinary.
As a prerequisite, sign up for a free account on Cloudinary. Note your cloud name and API keys on the dashboard.
Now do the following.
multerto accept zero arguments:
Instead of being written to a local directory, the uploaded files now reside in memory temporarily as a buffer.
Install Cloudinary’s Node.js SDK and the
npm install cloudinary npm install streamifier
streamifierrequired libraries in your codebase:
... const cloudinary = require('cloudinary').v2 const streamifier = require('streamifier') ...Code language: PHP (php)
/uploadendpoint code to upload files to Cloudinary:
Recall that you rewrote
multer earlier to process and store uploaded files temporarily as a buffer.
streamifier now converts the uploaded buffer to a readable stream, after which Cloudinary’s
upload_stream method streams directly to the cloud.
The returned result looks like this, complete with the details on the recently uploaded file:
Now you can fetch the URL from the JSON response and store it in the database. Mission accomplished.
Besides uploading images and videos, Cloudinary’s Node.js SDK can also transform, optimizatize, and deliver them. You can seamlessly integrate those capabilities with your Node.js app. For details, see the related documentation.
- Automating File Upload and Sharing
- Uploading PHP Files and Rich Media the Easy Way
- AJAX File Upload – Quick Tutorial & Time Saving Tips
- Impressed by WhatsApp technology? Clone WhatsApp Technology to Build a File Upload Android App
- Direct Image Uploads From the Browser to the Cloud With jQuery
- File Upload With Angular to Cloudinary
- Uploading Vue Files and Rich Media in Two Easy Steps
- Node.js File Upload To a Local Server Or to the Cloud
- Laravel File Upload to a Local Server Or to the Cloud