Metadata
When the Upload is in its asynchronous mode, the association between the files and the context they originate from is normally lost.
For example, in an application, the save handler has to associate the uploaded files with a specific message. The message and the file might be processed on different servers in a load-balancing or cloud-computing scenario.
The described approaches for sending and receiving metadata are also applicable to the
removehandler.
Sending Metadata
To send metadata over to the save handler:
-
Add an
inputfield for the file description whose value will be sent to thesavehandler.html<input type="text" id="fileDescription" /> -
Declare a handler for the
uploadevent and attach a data object to the passed event.jsfunction onUpload(e) { e.data = { fileDescription: $("#fileDescription").val() }; } -
Attach the
uploadevent handler.js$("#photos").kendoUpload({ async: { saveUrl: "saveHandler.php", removeUrl: "removeHandler.php" }, upload: onUpload }); -
Process the file and the associated description. The description and any other fields of the
e.dataobject will be serialized in the POST request. For more information on how to read posted form fields, refer to the documentation of your server-side platform.
Receiving Metadata
The save handler can sometimes produce a result that needs to be routed back to the page. The Upload requires a response in a JSON format with a Content-Type set to "text/plain". Responses that are not empty and in a format other than JSON will be treated as a server error.
To receive metadata from the save handler:
-
Build the response.
html<?php header('Content-Type: text/plain;'); $data = array('foo' => 'bar', 'status' => 'ok'); echo json_encode($data); ?> -
Declare a handler for the
successevent and process the response.jsfunction onSuccess(e) { alert("Status: " + e.response.status); } -
Attach the event handler.
js$("#photos").kendoUpload({ async: { saveUrl: "saveHandler.php", removeUrl: "removeHandler.php" }, success: onSuccess });