Skip to main content
A newer version of this page is available. .

DxUpload.Name Property

Specifies a unique identifier used to associate the Upload component with uploaded files on the server.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v21.1.dll

NuGet Package: DevExpress.Blazor

Declaration

[Parameter]
public string Name { get; set; }

Property Value

Type Description
String

A string value that specifies the identifier.

Remarks

Specify the Name property to associate the Upload component with the server. This property is used to access uploaded files on the server. You should also set the UploadUrl property to the path of a server-side controller’s action that processes upload requests.

<DxUpload Name="myFile" 
          UploadUrl="https://localhost:10000/api/Upload/UploadFile/">
</DxUpload>

On the server side, create a controller with an action that accepts the uploaded file, checks it, and saves it to the target location.

Do one of the following to access the uploaded file:

  • Create an action with a parameter whose name matches the Name property value.

    public ActionResult UploadFile(IFormFile myFile) {
        // ...
    }
    
  • Use the Name property value to get the uploaded file from form variables.

    public ActionResult UploadFile() {
        // ...
        var myFile = Request.Form.Files["myFile"];
        // ...
    }
    

The following example demonstrates how to implement the upload controller:

using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

[Route("api/[controller]")]
public class UploadController : ControllerBase {
    private readonly IWebHostEnvironment _hostingEnvironment;

    public UploadController(IWebHostEnvironment hostingEnvironment) {
        _hostingEnvironment = hostingEnvironment;
    }

    [HttpPost]
    [Route("UploadFile")]
    // "myFile" is the value of the Upload's "Name" property.
    public ActionResult UploadFile(IFormFile myFile) {
        try {
            // Specify the target location for the uploaded files.
            var path = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
            // Check whether the target directory exists; otherwise, create it.
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            using (var fileStream = System.IO.File.Create(Path.Combine(path, myFile.FileName))) {
                // Check the file here (its extension, safity, and so on). 
                // If all checks are passed, save the file.
                myFile.CopyTo(fileStream);
            }
        } catch {
            Response.StatusCode = 400;
        }

        return new EmptyResult();
    }
}

Run Demo: Upload - Overview

See Also