Getting Started with ASP.NET Core Uploader Control

23 Jul 20265 minutes to read

This section briefly explains how to include the ASP.NET Core Uploader control in your ASP.NET Core application using Visual Studio.

Create an ASP.NET Core Web App with Razor pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.

Install the required ASP.NET Core packages

To add ASP.NET Core Uploader control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetCore.Inputs and Syncfusion.AspNetCore.Themes packages. All Syncfusion ASP.NET Core packages are available on nuget.org. See the NuGet packages topic for details.

Alternatively, you can install the same packages using the Package Manager Console with the following command.

Install-Package Syncfusion.AspNetCore.Inputs -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add ASP.NET Core Tag Helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.Inputs Tag Helpers.

@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.Inputs

Add stylesheet and script resources

The theme stylesheet and script can be referenced from CDN. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml file.

<head>
    ...
    @* ASP.NET Core controls styles *@
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    @* ASP.NET Core controls scripts *@
    <script src="_content/Syncfusion.AspNetCore.Inputs/scripts/sf-uploader.min.js"></script>
</head>

Register the Script Manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.

<body>
    ...
    @* ASP.NET Core Script Manager *@
    <ejs-scripts></ejs-scripts>
</body>

Add ASP.NET Core Uploader control

Add the ASP.NET Core Uploader control in the ~/Pages/Index.cshtml file.

NOTE

From v16.2.41 version, the Essential JS2 AJAX library has been integrated for Uploader server requests.
Hence, use the third party promise library like blue-bird to use the Uploader in Internet Explorer.

<ejs-uploader id="uploadFiles" autoUpload="false"></ejs-uploader>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core Uploader control will render in your default web browser.

ASP.NET Core Uploader Control

Adding Drop Area

By default, the Uploader control allows to upload files by dragging files from File Explorer and dropping them into the Drop Area. You can configure any other external element as drop target using Drop Area property.

In the following sample, drop target is configured.

<div id='droparea'>Drop files here to upload</div>

<ejs-uploader id="uploadFiles" dropArea="#droparea" autoUpload="false"></ejs-uploader>

<style>
    .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%;
    }
</style>

Configure Asynchronous settings

The Uploader control processes the files to upload in Asynchronous mode by default. Define the properties saveUrl and removeUrl to handle the save and remove actions as follows.

@{
    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Save", RemoveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Remove" };
}

<ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" autoUpload="false"></ejs-uploader>

Handle Success and Failure Events

You can handle the Success and Failure events using the Success and Failure events. To handle these events, define the function and assign it to the corresponding event as follows.

@{
    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Save", RemoveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Remove" };
}
<ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" success="onUploadSuccess" failure="onUploadFailure" autoUpload="false"></ejs-uploader>
<script>
    function onUploadSuccess(args) {
        if (args.operation === 'upload') {
            console.log('success');
        }
    }
    function onUploadFailure(args) {
        console.log('failed');
    }
</script>

ASP.NET Core Uploader

NOTE

View Sample in GitHub.

See also

  1. Getting Started with ASP.NET Core using Razor Pages
  2. Getting Started with ASP.NET Core MVC using Tag Helper
  3. How to add additional data on upload
  4. Achieve file upload programmatically
  5. Achieve invisible upload