Skip to content

Commit ac4eb7c

Browse files
authored
983306: Re-structing the template section of File uploader.
1 parent a874723 commit ac4eb7c

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

blazor/file-upload/template.md

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
layout: post
33
title: Template in Blazor File Upload Component | Syncfusion
4-
description: Checkout and learn here about Template in Syncfusion Blazor File Upload component and much more details.
4+
description: Learn how to customize the Syncfusion Blazor File Upload component's appearance using templates, including how to add a progress bar.
55
platform: Blazor
66
control: File Upload
77
documentation: ug
88
---
99

1010
# Template in Blazor File Upload Component
1111

12-
The template in the code snippet allows for the customization of how file details are displayed in the uploader's UI. It provides flexibility to define the structure and styling of individual file elements, such as file name, size, and status. This allows to create a tailored and visually appealing file upload interface that aligns with their application's design and user experience requirements.
12+
The Blazor File Upload component allows for the customization of the file list items by using a template. This provides the flexibility to define the structure and styling of individual file elements, such as the file name, size, and status. A custom template can create a tailored and visually appealing file upload interface that aligns with an application's design and user experience requirements.
1313

1414
### With server-side API endpoint
1515

@@ -20,10 +20,12 @@ The template in the code snippet allows for the customization of how file detail
2020
<UploaderAsyncSettings SaveUrl="https://blazor.syncfusion.com/services/production/api/FileUploader/Save"
2121
RemoveUrl="https://blazor.syncfusion.com/services/production/api/FileUploader/Remove"></UploaderAsyncSettings>
2222
<UploaderTemplates>
23-
<Template>
24-
<div class="name file-name" title="@(context.Name)">File Name : @(context.Name)</div>
25-
<div class="file-size">File Size : @(context.Size)</div>
26-
<div class="e-file-status">File Status : @(context.Status)</div>
23+
<Template Context="context">
24+
<div style="padding: 10px;">
25+
<div class="name file-name" title="@context.Name">File Name : @context.Name</div>
26+
<div class="file-size">File Size : @context.Size</div>
27+
<div class="e-file-status">File Status : @context.Status</div>
28+
</div>
2729
</Template>
2830
</UploaderTemplates>
2931
</SfUploader>
@@ -35,28 +37,31 @@ The template in the code snippet allows for the customization of how file detail
3537
3638
<SfUploader ID="Files" AutoUpload="false">
3739
<UploaderTemplates>
38-
<Template>
39-
<div class="name file-name" title="@(context.Name)">File Name : @(context.Name)</div>
40-
<div class="file-size">File Size : @(context.Size)</div>
41-
<div class="e-file-status">File Status : @(context.Status)</div>
40+
<Template Context="context">
41+
<div style="padding: 10px;">
42+
<div class="name file-name" title="@context.Name">File Name : @context.Name</div>
43+
<div class="file-size">File Size : @context.Size</div>
44+
<div class="e-file-status">File Status : @context.Status</div>
45+
</div>
4246
</Template>
4347
</UploaderTemplates>
4448
</SfUploader>
4549
```
4650

47-
### Adding progressbar using template
51+
## Adding a progress bar using a template
4852

49-
When using the file upload template, the progress bar does not display in the UI. We can add the progress bar using template and then show the progress while reading and writing to the stream in the ValueChange event. The code snippet below demonstrates a Blazor file upload example with a progress bar using a custom template. The custom template includes elements to display the file name, size, progress bar, and file status.
53+
When using a file upload template, the default progress bar is not displayed in the UI. A progress bar can be added within the template, and its progress can be updated by reading and writing to the stream in the `ValueChange` event. The following code snippet demonstrates how to add a progress bar to a Blazor File Upload component using a custom template. The custom template includes elements to display the file name, size, progress bar, and file status.
5054

5155
```cshtml
5256
@using Syncfusion.Blazor.Inputs
5357
@using Syncfusion.Blazor.ProgressBar
54-
<SfUploader @ref="@uploderObj" ID="UploadFiles" MaxFileSize="100000000000" ShowProgressBar = "false">
55-
<UploaderEvents BeforeUpload="onBeforeUpload" Success="onSuccess" ValueChange="onChange"></UploaderEvents>
58+
59+
<SfUploader @ref="@uploaderObj" ID="UploadFiles" MaxFileSize="100000000000" ShowProgressBar="false">
60+
<UploaderEvents BeforeUpload="OnBeforeUpload" Success="OnSuccess" ValueChange="OnChange"></UploaderEvents>
5661
<UploaderTemplates>
57-
<Template>
62+
<Template Context="context">
5863
<span class="e-file-container">
59-
<span class="e-file-name" title="@(context.Name)">@this.GetFileName(context.Name)</span>
64+
<span class="e-file-name" title="@context.Name">@GetFileName(context.Name)</span>
6065
<span class="e-file-type">@("." + context.Type)</span>
6166
@if (context.Size > 0)
6267
{
@@ -67,9 +72,9 @@ When using the file upload template, the progress bar does not display in the UI
6772
{
6873
<span class="e-upload-progress-wrap">
6974
<span class="e-progress-inner-wrap">
70-
<progressbar class="@("e-upload-progress-bar" + " " + "e-upload-progress")" value="0" max="100" style="width: @(ProgressValue.ToString() + "%")"></progressbar>
75+
<progress class="@("e-upload-progress-bar" + " " + "e-upload-progress")" value="0" max="100" style="width: @(ProgressValue + "%")"></progress>
7176
</span>
72-
<span class="e-progress-bar-text">@(ProgressValue.ToString() + "%")</span>
77+
<span class="e-progress-bar-text">@(ProgressValue + "%")</span>
7378
</span>
7479
}
7580
@if (fileStates.ContainsKey(context.Name) && fileStates[context.Name] && context.Status == "File uploaded successfully")
@@ -86,112 +91,103 @@ When using the file upload template, the progress bar does not display in the UI
8691
}
8792
@if (fileStates.ContainsKey(context.Name) && fileStates[context.Name] && context.Status == "File uploaded successfully")
8893
{
89-
<span class="e-icons e-file-delete-btn @this.RemoveIconDisable" id="deleteIcon" title="Delete" @onclick="(args) => ClickHandler(context)"></span>
94+
<span class="e-icons e-file-delete-btn @RemoveIconDisable" id="deleteIcon" title="Delete" @onclick="(args) => ClickHandler(context)"></span>
9095
}
9196
else
9297
{
93-
<span class="e-icons e-file-remove-btn @this.RemoveIconDisable" id="removeIcon" title="Remove" @onclick="(args) => ClickHandler(context)"></span>
98+
<span class="e-icons e-file-remove-btn @RemoveIconDisable" id="removeIcon" title="Remove" @onclick="(args) => ClickHandler(context)"></span>
9499
}
95100
</span>
96-
97101
</Template>
98102
</UploaderTemplates>
99103
</SfUploader>
104+
100105
@code {
101-
private SfUploader uploderObj;
102-
public bool isVisible { get; set; } = false;
103-
private FileInfo CurrentFile {get; set;}
106+
private SfUploader uploaderObj;
107+
private FileInfo CurrentFile { get; set; }
104108
public string fileSize { get; set; } = "0";
105109
public string RemoveIconDisable = string.Empty;
106-
#pragma warning disable CA2213 // Disposable fields should be disposed
107110
private readonly SemaphoreSlim FileSemaphore = new SemaphoreSlim(1);
108-
#pragma warning restore CA2213 // Disposable fields should be disposed
109111
private decimal ProgressValue { get; set; } = 0;
110112
private Dictionary<string, bool> fileStates = new Dictionary<string, bool>();
113+
111114
public string CalculateFileSize(double size)
112115
{
113-
string fileSizeStr = "";
114116
if (size >= 1024 * 1024)
115117
{
116-
return fileSizeStr = $"{size / (1024f * 1024f):0.00} MB";
118+
return $"{size / (1024f * 1024f):0.00} MB";
117119
}
118120
else if (size >= 1024)
119121
{
120-
return fileSizeStr = $"{size / 1024f:0.00} KB";
122+
return $"{size / 1024f:0.00} KB";
121123
}
122124
else
123125
{
124-
return fileSizeStr = $"{size} bytes";
126+
return $"{size} bytes";
125127
}
126128
}
129+
127130
private string GetFileName(string fileName)
128131
{
129132
string type = GetFileType(fileName);
130133
string[] names = fileName.Split(new string[] { "." + type }, StringSplitOptions.None);
131134
return names[0];
132135
}
136+
133137
private string GetFileType(string name)
134-
#pragma warning restore CA1822 // Mark members as static
135138
{
136139
string extension = string.Empty;
137140
int index = name.LastIndexOf('.');
138141
if (index > 0)
139142
{
140143
extension = name.Substring(index + 1);
141144
}
142-
143-
return (!string.IsNullOrEmpty(extension)) ? extension : string.Empty;
144-
}
145-
private async Task onFileRemove()
146-
{
147-
await uploderObj.RemoveAsync();
145+
return !string.IsNullOrEmpty(extension) ? extension : string.Empty;
148146
}
147+
149148
public async Task ClickHandler(FileInfo context)
150149
{
151-
await uploderObj.RemoveAsync(new FileInfo[] { context });
150+
await uploaderObj.RemoveAsync(new FileInfo[] { context });
152151
}
153-
public void onBeforeUpload(BeforeUploadEventArgs args)
152+
153+
public void OnBeforeUpload(BeforeUploadEventArgs args)
154154
{
155155
foreach (var file in args.FilesData)
156156
{
157157
fileStates[file.Name] = false;
158158
}
159159
}
160-
public void onSuccess(SuccessEventArgs args)
160+
161+
public void OnSuccess(SuccessEventArgs args)
161162
{
162-
//fileStates[args.File.Name] = false;
163-
isVisible = false;
164-
this.ProgressValue = 0;
163+
ProgressValue = 0;
165164
}
166-
167-
public async Task onChange(UploadChangeEventArgs args)
165+
166+
public async Task OnChange(UploadChangeEventArgs args)
168167
{
169168
await FileSemaphore.WaitAsync();
170169
try
171170
{
172171
foreach (var file in args.Files)
173172
{
174-
var path = @"" + file.FileInfo.Name;
173+
var path = Path.Combine(Path.GetTempPath(), file.FileInfo.Name);
175174
CurrentFile = file.FileInfo;
176175
CurrentFile.Status = "Uploading";
177176
await using FileStream writeStream = new(path, FileMode.Create);
178177
using var readStream = file.File.OpenReadStream(file.File.Size);
179178
var bytesRead = 0;
180-
var totalRead = 0;
181179
var buffer = new byte[1024 * 10];
182180
183181
while ((bytesRead = await readStream.ReadAsync(buffer)) != 0)
184182
{
185183
RemoveIconDisable = "e-disabled";
186-
totalRead += bytesRead;
187184
await writeStream.WriteAsync(buffer, 0, bytesRead);
188185
ProgressValue = (long)((decimal)readStream.Position * 100) / readStream.Length;
189186
StateHasChanged();
190187
}
191188
CurrentFile.Status = "File uploaded successfully";
192189
RemoveIconDisable = string.Empty;
193190
fileStates[file.FileInfo.Name] = true;
194-
195191
}
196192
}
197193
catch (Exception ex)
@@ -207,4 +203,4 @@ When using the file upload template, the progress bar does not display in the UI
207203
}
208204
```
209205

210-
![Blazor FileUpload with Chunk Upload](./images/blazor-template-progressbar.gif)
206+
![Blazor File Upload with a custom template and progress bar](./images/blazor-template-progressbar.gif)

0 commit comments

Comments
 (0)