Back to Devexpress

DxUpload.FileUploadStart Event

blazor-devexpress-dot-blazor-dot-dxupload-46a0dd0c.md

latest7.0 KB
Original Source

DxUpload.FileUploadStart Event

Fires when file upload is about to start.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[Parameter]
public EventCallback<FileUploadStartEventArgs> FileUploadStart { get; set; }

Event Data

The FileUploadStart event's data class is FileUploadStartEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelSpecifies whether the file upload operation should be canceled.
FileInfoReturns information about a file. Inherited from FileUploadEventArgs.
RequestDataSpecifies the upload request’s custom data.
RequestHeadersSpecifies the upload request’s headers.
UploadUrlSpecifies a target URL for the upload request.

Remarks

In Instant upload mode, the FileUploadStart event occurs when a user selects a file in the Open File dialog or drops a file onto the drop zone. In OnButtonClick upload mode, this event fires when a user clicks the upload button.

Handle the FileUploadStart event to check file upload before it starts. For instance, you can add authentication information to the AJAX request, cancel upload based on a condition, or change the upload URL. Use the event argument’s FileInfo property to get information about the file (its name, type, size, and so on).

If the file upload operation is not cancelled, the FileUploadStarted event occurs after the FileUploadStart event.

Add Request Headers and Data

Use the event argument’s RequestHeaders and RequestData properties to specify headers and custom data for the upload request. The following snippet adds an authentication token and username to the request and uses their values in the server-side controller.

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

@code {
    void OnFileUploadStart(FileUploadStartEventArgs e) {
        e.RequestHeaders.Add("Authorization", $"Bearer {MySecretToken}");
        e.RequestData.Add("Username", "MyUsername");
    }
}
csharp
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

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

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

    [HttpPost]
    [Authorize]
    [Route("UploadFile")]
    public ActionResult UploadFile(IFormFile myFile) {
        string username = Request.Form["Username"];
        // Implement additional validation here.
        // ...
    }
}
csharp
// ...
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authentication.JwtBearer;
//...
builder.Services.AddAuthentication(options => {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
});
//...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
//...

Cancel File Upload

Set the event argument’s Cancel property to true to cancel file upload. The following snippet prohibits the upload of files that exceed 1MB:

razor
<DxUpload Name="myFile" UploadUrl="https://localhost:10000/api/Upload/Upload/" 
          FileUploadStart="@OnFileUploadStart">
</DxUpload>

@code {
    void OnFileUploadStart(FileUploadStartEventArgs e) {
        if (e.FileInfo.Size > 1048576)
            e.Cancel = true;
    }
}
csharp
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BlazorDemo.AspNetCoreHost;
[Route("api/[controller]")]
[ApiController]
public class UploadController : ControllerBase {
    [HttpPost("[action]")]
    public ActionResult Upload(IFormFile myFile) {
        try {
            // Write code that saves the 'myFile' file.
            // Don't rely on or trust the FileName property without validation.
        } catch {
            return BadRequest();
        }
        return Ok();
    }
}

Change an Upload URL

The Upload component’s UploadUrl property specifies a path of a server-side controller’s action that processes all upload requests. Use the event argument’s UploadUrl property to change the controller action based on conditions. The snippet below changes the upload URL for PNG files.

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

@code {
    void OnFileUploadStart(FileUploadStartEventArgs e) {
        if (e.FileInfo.Type == "image/png")
            e.UploadUrl = "https://localhost:10000/api/Upload/UploadPng/";
    }
}
csharp
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")]
    public ActionResult UploadFile(IFormFile myFile) {
        // ...
    }

    [HttpPost]
    [Route("UploadPng")]
    public ActionResult UploadPng(IFormFile myFile) {
        try {
            // Write code that saves the 'myFile' file.
            // Don't rely on or trust the FileName property without validation.
        } catch {
            return BadRequest();
        }
        return Ok();
    }
}

See Also

DxUpload Class

DxUpload Members

DevExpress.Blazor Namespace