Skip to content

Commit ba0bd64

Browse files
authored
docs(upload): Show how to send complex objects in OnUpload (#2629)
1 parent be3e8a1 commit ba0bd64

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

components/upload/events.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,17 @@ Use the `OnUpload` and [`OnRemove`](#onremove) event handlers to send additional
419419
* [CSRF/XSRF cross-site antiforgery tokens]({%slug upload-kb-validateantiforgerytoken%})
420420
* Any metadata related to the app business logic
421421

422-
To send **cookies** with the upload request, set the [`WithCredentials` component parameter]({%slug upload-overview%}#upload-parameters) to `true`.
422+
To send cookies with the upload request, set the [`WithCredentials` component parameter]({%slug upload-overview%}#upload-parameters) to `true`.
423+
424+
To send a complex object or a collection, serialize it first. Receive it as a `string` argument in the controller method and deserialize it.
423425

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

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

428430
````Razor
431+
@using System.Text.Json
432+
429433
<TelerikUpload OnUpload="@OnUploadHandler" />
430434

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

439-
args.RequestData.Add("dataKey", "dataValue"); // for example, user name
440-
args.RequestHeaders.Add("headerKey", "headerValue"); // for example, authentication token
443+
string[] collection = { "foo", "bar", "baz" };
444+
445+
args.RequestHeaders.Add("headerKey", "headerValue"); // for example, token
446+
args.RequestData.Add("dataKey", "dataValue"); // for example, new file name
447+
args.RequestData.Add("collectionKey", JsonSerializer.Serialize(collection));
441448
}
442449
}
443450
````
444451
````Controller
452+
using System.Text.Json;
453+
445454
// Get the custom data and header values from additional method arguments
446455
[HttpPost]
447-
public async Task<IActionResult> Save(IFormFile files, [FromForm] string dataKey, [FromHeader] string headerKey)
456+
public async Task<IActionResult> Save(
457+
IFormFile files,
458+
[FromHeader] string headerKey,
459+
[FromForm] string dataKey,
460+
[FromForm] string collectionKey)
448461
{
449462
// ...
450463
451-
string customData = dataKey;
452464
string customHeader = headerKey;
465+
string customData = dataKey;
466+
string[]? customCollection = JsonSerializer.Deserialize<string[]>(collectionKey);
453467

454468
// ...
455469
}
@@ -462,8 +476,13 @@ public async Task<IActionResult> Save(IFormFile files)
462476
{
463477
// ...
464478
465-
string customData = Request.Form["dataKey"];
466-
string customHeader = Request.Headers["headerKey"];
479+
string? customHeader = Request.Headers["headerKey"];
480+
string? customData = Request.Form["dataKey"];
481+
482+
if (Request.Form.ContainsKey("collectionKey"))
483+
{
484+
string[]? customCollection = JsonSerializer.Deserialize<string[]>(Request.Form["collectionKey"]!);
485+
}
467486

468487
// ...
469488
}

0 commit comments

Comments
 (0)