Skip to content

About

Use the DevExtreme FileUploader to upload images and display links for these images in an ASP.NET Core Razor Pages application.

Topics

Resources

Stars

5 stars

Watchers

54 watching

Forks

Repository files navigation

File Uploader for ASP.NET Core - How to upload images

This example uses the DevExtreme FileUploader to upload images and display links for these images in an ASP.NET Core MVC application with chunked upload support.

Files to Review

Implementation Details

Note The project targets .NET 10+ and uses modern ASP.NET Core MVC architecture with System.Text.Json for JSON serialization.

  1. Configure a Visual Studio Project.

  2. Add the FileUploader component to your MVC View. Specify its Name and UploadUrl properties.

    @(Html.DevExtreme().FileUploader()
        .Name("uploadedFile")
        .ChunkSize(200000)
        .Multiple(true)
        .AllowedFileExtensions(new[] { ".jpg", ".jpeg", ".gif", ".png" })
        .MaxFileSize(4000000)
        .UploadUrl(Url.Action("Upload", "FileUpload"))
  3. Create a dedicated controller for file upload operations and implement the Chunk Upload technique. The controller uses System.Text.Json for JSON deserialization.

  4. Add the token value to the file uploader's requests because ASP.NET Core validates the Antiforgery Token in POST requests.

    .UploadCustomData(new JS("{__RequestVerificationToken: document.getElementsByName('__RequestVerificationToken')[0].value}"))
  5. The FileUploadController processes chunked uploads using System.Text.Json for metadata deserialization and handles file validation, temporary file management, and final file processing.

    [HttpPost]
    public IActionResult Upload(IFormFile uploadedFile, string chunkMetadata)
    {
        var tempPath = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
        var resultImageUrl = string.Empty;
        try
        {
            if (!string.IsNullOrEmpty(chunkMetadata))
            {
                var metaDataObject = JsonSerializer.Deserialize<ChunkMetadata>(chunkMetadata);
                // Process chunk upload...
            }
        }
        catch
        {
            return BadRequest();
        }
        return new JsonResult(new { imgUrl = resultImageUrl });
    }
  6. (Optional) Handle the FileUploader.OnUploaded event and get an image URL from the args.request.response property.

    function OnUploaded(args) {
            let imgUrl = JSON.parse(args.request.response).imgUrl;
            let imgName = args.file.name;
            let imagesContainer = document.getElementById("imagesContainer");
            imagesContainer.appendChild(createLink(imgName, imgUrl));
        }
    function createLink(imgName, url) {
            let link = document.createElement("A");
            link.innerText = imgName;
            link.href = url;
            return link;
        }

Documentation

Upload Files: Server-Side Implementation in ASP.NET

More Examples

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Use the DevExtreme FileUploader to upload images and display links for these images in an ASP.NET Core Razor Pages application.

Topics

Resources

Stars

5 stars

Watchers

54 watching

Forks

Used by

Contributors

Languages