Chunk Upload
Chunk upload works by splitting a file into smaller parts (chunks) and sending them one by one in several sequential requests. These chunks are then reassembled at the remote endpoint into the final file.
Enable Chunk Upload
To enable the chunk upload feature, add the <UploadChunkSettings> tag inside inside <UploadSettings>. See the UploadChunkSettings API reference for all avalable parameters.
<TelerikUpload>
<UploadSettings>
<UploadChunkSettings AutoRetryAfter="100"
Enabled="true"
MaxAutoRetries="1"
MetadataField="chunkMetadata"
Resumable="true"
Size="@(1024 * 1024)" />
</UploadSettings>
</TelerikUpload>
The above snippet shows the default values explicitly. It is equivalent to an empty <UploadChunkSettings> tag:
<TelerikUpload>
<UploadSettings>
<UploadChunkSettings />
</UploadSettings>
</TelerikUpload>
Meta Data
When posting files in chunks, the Upload component sends each chunk together with serialized meta data to the controller. To deserialize this meta data, define a class with the following properties. You can name the class, according to your preferences.
using using System.Runtime.Serialization;
[DataContract]
public class ChunkMetadata
{
[DataMember(Name = "fileId")]
public string FileId { get; set; } = string.Empty;
[DataMember(Name = "fileName")]
public string FileName { get; set; } = string.Empty;
[DataMember(Name = "fileSize")]
public long FileSize { get; set; }
[DataMember(Name = "contentType")]
public string ContentType { get; set; } = string.Empty;
[DataMember(Name = "chunkIndex")]
public long ChunkIndex { get; set; }
[DataMember(Name = "totalChunks")]
public long TotalChunks { get; set; }
}
Controller Implementation
The controller method that receives file chunks should be similar to the regular controller method that receives complete files. The major differences are:
- The Save action method must expect an additional
stringargument with a name that matches the value of theUploadChunkSettingsMetadataFieldparameter. By default, that is"chunkMetadata". - The action method must obtain the uploaded file name from the
FileNameproperty of the meta data object, instead of theFileNameproperty of theIFormFileargument. - The
FileStreamFileModemust beAppendinstead ofCreatefor all chunks, except the first one. Check theChunkIndexproperty of the meta data object.
The Upload sends the next chunk only after the previous one has been uploaded successfully.
As always, make sure that the Save action method name is consistent with the Upload SaveUrl parameter value.
Chunk upload controller action method
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public async Task<IActionResult> SaveChunk(IFormFile files, [FromForm] string chunkMetadata)
{
DataContractJsonSerializer dcSerializer = new(typeof(ChunkMetadata));
MemoryStream ms = new(Encoding.UTF8.GetBytes(chunkMetadata));
string saveLocation = Path.Combine(HostingEnvironment.WebRootPath, metadata.FileName);
using FileStream fs = new(saveLocation, metadata.ChunkIndex == 0 ? FileMode.Create : FileMode.Append);
await files.CopyToAsync(fs);
// ...
}
Events
The Upload exposes events related to chunk uploading. See the examples in the Upload Events article.
OnPause—fires when the user clicks on the Pause button during chunk upload.OnResume—fires when the user clicks on the Resume button during chunk upload.
The Upload renders Pause and Resume buttons only when Resumable is true in the UploadChunkSettings, which is by default.
Example
The UploadController class below assumes that the project name and namespace is TelerikBlazorApp.
Make sure to enable controller routing in the app startup file (Program.cs). In this case, the file includes the following lines:
builder.Services.AddControllers();app.MapDefaultControllerRoute();.
Also see:
- Section Implement Controller Methods
- Page Upload Troubleshooting
Using Chunk Upload
@inject NavigationManager NavigationManager
<TelerikUpload SaveUrl="@SaveChunkUrl"
RemoveUrl="@RemoveUrl">
<UploadSettings>
<UploadChunkSettings Size="@(512 * 1024)"
AutoRetryAfter="300"
MaxAutoRetries="2"
MetadataField="chunkMetadata">
</UploadChunkSettings>
</UploadSettings>
</TelerikUpload>
@code {
private string SaveChunkUrl => NavigationManager.ToAbsoluteUri("api/upload/savechunk").ToString();
private string RemoveUrl => NavigationManager.ToAbsoluteUri("api/upload/remove").ToString();
}