Skip to content

Commit 0982da7

Browse files
docs(upload): example for renaming files and checking for duplicates
1 parent 72903ef commit 0982da7

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

components/upload/events.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,61 @@ You can cancel the event based on a condition (for example, some information abo
7070
}
7171
````
7272

73+
74+
>caption Changing the file name and checking for duplicate files on the server
75+
76+
In some cases, you may have duplicate files with the same name on the server and you may need to change the current file name when uploading it. This must happen in the endpoint only, and there are a couple of ways to notify the client for that:
77+
78+
* Use the [OnSuccess event](#onsuccess) to read information the endpoint will send so you can use it in the view (for example, to add a preview of an image). In this case, the file name in the Upload component will not change.
79+
80+
* Use the OnSelect event to call the endpoint and check for duplicates before the actual upload process starts. You can generate a new name, if needed, and set it to the `Name` of the file that will be uploaded. This will update the rendered file name in the UI for the user. Note that this will not change the file name that will be sent to the endpoint - it will still be the original file name from the user's file system and it is up to the endpoint to implement the same logic when saving the file.
81+
82+
Of course, you can combine both approaches.
83+
84+
85+
````CSHTML
86+
@inject NavigationManager NavigationManager
87+
88+
<TelerikUpload SaveUrl="@SaveUrl"
89+
RemoveUrl="@RemoveUrl"
90+
OnSelect="@OnSelectHandler">
91+
</TelerikUpload>
92+
93+
@code {
94+
async Task OnSelectHandler(UploadSelectEventArgs e)
95+
{
96+
foreach (var item in e.Files)
97+
{
98+
// this will change the file name displayed in the TelerikUpload component
99+
// delays will result in a delay in rendering the file and starting the upload
100+
// NOTE: the file name in the XHR request to the server will be the original file name
101+
// and it is up to the server to handle it with the same logic for naming
102+
item.Name = await AskServerForFinalFileName(item.Name, "currentUserName");
103+
}
104+
}
105+
106+
async Task<string> AskServerForFinalFileName(string fileName, string userName)
107+
{
108+
await Task.Delay(500); // simulate network delay. Remove for a real app
109+
110+
// in a real case this can be the controller that will save the files on the server
111+
// make sure that the same name generation logic will be used when actually saving the file
112+
string finalName = $"{userName}-{fileName}";
113+
114+
return await Task.FromResult(finalName);
115+
}
116+
117+
// a sample way of generating the URLs to the endpoint
118+
public string SaveUrl => ToAbsoluteUrl("api/upload/save");
119+
public string RemoveUrl => ToAbsoluteUrl("api/upload/remove");
120+
121+
public string ToAbsoluteUrl(string url)
122+
{
123+
return $"{NavigationManager.BaseUri}{url}";
124+
}
125+
}
126+
````
127+
73128
@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async)
74129

75130

0 commit comments

Comments
 (0)