DxUpload.ChunkSize Property
Specifies the chunk size in bytes.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v26.1.dll
NuGet Package: DevExpress.Blazor
Declaration
[Parameter]
public int ChunkSize { get; set; }
Property Value
| Type | Description |
|---|---|
| Int32 | The chunk size in bytes. |
Remarks
The Upload can split large files into small packets and send them to the server in multiple requests (one by one). To enable chunk upload, set the ChunkSize property to a positive value that specifies the packet size in bytes.
<DxUpload Name="myFile"
UploadUrl="https://localhost:10000/api/Upload/UploadChunkFile/"
ChunkSize="1000000" />
On the server, configure your controller to process file chunks. To access the uploaded file, use the Upload’s Name property value. To obtain information about the file chunk, use a JSON object with the same structure as the ChunkMetadata class in the following code snippet.
To access file and chunk metadata, use the following ways:
Create an action with parameters. The first parameter’s name should match the
Nameproperty value. The second parameter should be a string that defines chunk metadata serialized to JSON.public ActionResult UploadChunkFile(IFormFile myFile, [FromForm] string chunkMetadata) { // ... var metaDataObject = JsonConvert.DeserializeObject<ChunkMetadata>(chunkMetadata); // ... }Get the uploaded file and chunk metadata from form variables.
public ActionResult UploadChunkFile() { // ... var myFile = Request.Form.Files["myFile"]; var chunkMetadata = Request.Form["chunkMetadata"]; var metaDataObject = JsonConvert.DeserializeObject<ChunkMetadata>(chunkMetadata); // ... }
In the upload action, merge chunks and save the resulting file to the target location.
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
// Declare a class that stores chunk details.
public class ChunkMetadata {
public int Index { get; set; }
public int TotalCount { get; set; }
public long FileSize { get; set; }
public string FileName { get; set; }
public string FileType { get; set; }
public Guid FileGuid { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class UploadChunksController : ControllerBase {
[HttpPost("[action]")]
public ActionResult Upload(IFormFile myFile) {
string chunkMetadata = Request.Form["chunkMetadata"];
try {
if(!string.IsNullOrEmpty(chunkMetadata)) {
var metaDataObject = JsonSerializer.Deserialize<ChunkMetadata>(chunkMetadata);
if(metaDataObject.FileGuid == Guid.Empty)
return BadRequest();
/* Append the myFile chunk to a temporary file in a dedicated upload directory.
You can use $"{metaDataObject.FileGuid}.tmp" as the temporary file name.
The client sends all metadata values. Treat these values as untrusted input.
Enforce the total file size limit on the server.
Reject the incoming chunk if the combined size of the temporary file and the chunk
exceeds the configured limit.
Do not trust the FileName property without validation. */
if(metaDataObject.Index == metaDataObject.TotalCount - 1) {
/* Write code that saves the 'myFile' file.
Do not trust the FileName property without validation. */
}
}
} catch {
return BadRequest();
}
return Ok();
}
}
Users can pause or cancel file upload, if the AllowPause or AllowCancel property is set to true.
