Getting Started with EJ2 JavaScript File Upload
21 Nov 202615 minutes to read
This section explains the steps required to create a simple Essential® JS 2 Uploader and demonstrate the basic usage of the Uploader control in a JavaScript application.
Dependencies
The list of dependencies required to use the Uploader component in your application is given below:
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-buttonsSetup for local environment
Refer to the following steps to set up your local environment.
Step 1: Create a root folder named my-app for your application.
Step 2: Create a my-app/resources folder to store local scripts and styles files.
Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Uploader control.
Control Initialization
The Essential® JS 2 JavaScript controls can be initialized by using below css styles and Scripts
Add Syncfusion® Uploader CSS Styles and Scripts
Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base scripts are required dependency packages for the Uploader component.
Styles(CSS):
https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.cssScripts (javascript):
https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.jsInitialize the uploader
The uploader can be initialized through an input tag. Add an HTML input element to be initialized as an uploader in your index.html file.
[src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Uploader component</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<meta name="author" content="Syncfusion" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
<script src="system.config.js" type="text/javascript"></script>
</head>
<body>
<div id='container' style="margin:0 auto; width:300px;">
<!--element which is going to render the Uploader-->
<input type="file" id='fileupload' />
</div>
</body>
</html>The Custom Resource Generator (CRG) is an online web tool, which can be used to generate the custom script and styles for a set of specific components.
This web tool is useful to combine the required component scripts and styles in a single file.
Now, import the uploader component in your app.ts file and initialize it with the #uploader element.
[src/app/app.ts]
// initialize Uploader component
var uploadObject = new ej.inputs.Uploader();
// render initialized Uploader
uploadObject.appendTo('#uploader');Run the application
After completing the configuration to render the basic uploader, run the following command to display the output in your default browser.
npm run start
From v16.2.41 version, the Essential® JS2 AJAX library has been integrated for uploader server requests. Hence, use the third party
promiselibrary like blue-bird to use the uploader in Internet Explorer.
The following example illustrates the output in your browser.
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 UPloader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 Uploader Component">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" class="fileupload">
<input type="file" id="fileupload" name="UploadFiles">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>#container {
visibility: hidden;
margin: 0 auto;
width: 400px;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}Adding drop area
By default, the uploader component allows to upload files by drag the files from file explorer, and drop into the drop area. You can configure any other external element as drop target using dropArea property.
In the following sample, drop target is configured.
// initialize Uploader component
var uploadObject = new ej.inputs.Uploader({
autoUpload: false,
dropArea: document.getElementById('droparea')
});
// render initialized Uploader
uploadObject.appendTo('#fileupload');<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 Uploader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 Uploader Component">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="width: 90%">
<div id="droparea">
Drop files here to upload
</div>
<div id="uploadfile">
<input type="file" id="fileupload" name="UploadFiles">
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>#container {
visibility: hidden;
padding-left: 5%;
width: 100%;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
.fileupload {
margin: 20px auto;
width: 400px;
}
#droparea {
padding: 50px 25px;
margin: 30px auto;
border: 1px solid #c3c3c3;
text-align: center;
width: 20%;
display: inline-flex;
}
.e-file-select,
.e-file-drop {
display: none;
}
body .e-upload-drag-hover {
outline: 2px dashed brown;
}
#uploadfile {
width: 60%;
display: inline-flex;
margin-left: 5%;
}Configure asynchronous settings
The uploader component process the files to upload in Asynchronous mode by default. Define the properties saveUrl and removeUrl to handle the save and remove action as follows.
// initialize Uploader component
var uploadObject = new ej.inputs.Uploader({
asyncSettings: {
saveUrl: 'https://services.syncfusion.com/js/production/api/FileUploader/Save',
removeUrl: 'https://services.syncfusion.com/js/production/api/FileUploader/Remove'
}
});
// render initialized Uploader
uploadObject.appendTo('#fileupload');<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 Uploader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 Uploader Component">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<input type="file" id="fileupload" name="UploadFiles">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>#container {
visibility: hidden;
margin: 0 auto;
width: 400px;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}Handle success and failed upload
You can handle the success and failure actions using the success and failure events. To handle these event, define the function and assign it to corresponding event as follows.
// initialize Uploader component
var uploadObject = new ej.inputs.Uploader({
asyncSettings: {
saveUrl: 'https://services.syncfusion.com/js/production/api/FileUploader/Save',
removeUrl: 'https://services.syncfusion.com/js/production/api/FileUploader/Remove'
},
success: onUploadSuccess,
failure: onUploadFailure
});
// render initialized Uploader
uploadObject.appendTo('#fileupload');
function onUploadSuccess(args) {
if (args.operation === 'upload') {
console.log('File uploaded successfully');
}
}
function onUploadFailure(args) {
console.log('File failed to upload');
}<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 Uploader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 Uploader Component">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/uploader/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<input type="file" id="fileupload" name="UploadFiles">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>#container {
visibility: hidden;
margin: 0 auto;
width: 400px;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}You can also explore JavaScript File Upload feature tour page for its groundbreaking features. You can also explore our JavaScript File Upload example that shows how to render the file upload and browse the files which you want to upload to the server.