Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge onupload-list-2629 into production #2636

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions components/upload/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,17 @@ Use the `OnUpload` and [`OnRemove`](#onremove) event handlers to send additional
* [CSRF/XSRF cross-site antiforgery tokens]({%slug upload-kb-validateantiforgerytoken%})
* Any metadata related to the app business logic

To send **cookies** with the upload request, set the [`WithCredentials` component parameter]({%slug upload-overview%}#upload-parameters) to `true`.
To send cookies with the upload request, set the [`WithCredentials` component parameter]({%slug upload-overview%}#upload-parameters) to `true`.

To send a complex object or a collection, serialize it first. Receive it as a `string` argument in the controller method and deserialize it.

>caption Using the OnUpload event to send custom data to the controller

<div class="skip-repl"></div>

````Razor
@using System.Text.Json

<TelerikUpload OnUpload="@OnUploadHandler" />

@code {
Expand All @@ -436,20 +440,30 @@ To send **cookies** with the upload request, set the [`WithCredentials` componen
args.IsCancelled = true;
}

args.RequestData.Add("dataKey", "dataValue"); // for example, user name
args.RequestHeaders.Add("headerKey", "headerValue"); // for example, authentication token
string[] collection = { "foo", "bar", "baz" };

args.RequestHeaders.Add("headerKey", "headerValue"); // for example, token
args.RequestData.Add("dataKey", "dataValue"); // for example, new file name
args.RequestData.Add("collectionKey", JsonSerializer.Serialize(collection));
}
}
````
````Controller
using System.Text.Json;

// Get the custom data and header values from additional method arguments
[HttpPost]
public async Task<IActionResult> Save(IFormFile files, [FromForm] string dataKey, [FromHeader] string headerKey)
public async Task<IActionResult> Save(
IFormFile files,
[FromHeader] string headerKey,
[FromForm] string dataKey,
[FromForm] string collectionKey)
{
// ...

string customData = dataKey;
string customHeader = headerKey;
string customData = dataKey;
string[]? customCollection = JsonSerializer.Deserialize<string[]>(collectionKey);

// ...
}
Expand All @@ -462,8 +476,13 @@ public async Task<IActionResult> Save(IFormFile files)
{
// ...

string customData = Request.Form["dataKey"];
string customHeader = Request.Headers["headerKey"];
string? customHeader = Request.Headers["headerKey"];
string? customData = Request.Form["dataKey"];

if (Request.Form.ContainsKey("collectionKey"))
{
string[]? customCollection = JsonSerializer.Deserialize<string[]>(Request.Form["collectionKey"]!);
}

// ...
}
Expand Down
Loading