Upload Service Parameters
Sometimes you may need to pass custom parameters to the upload service.
To learn how to read the parameters on the server-side, read the last section of the topic.
Upload-specific parameters
If you want to pass parameters, that will be sent with every request to the upload handler, for all the files that will be uploaded, you can use the AdditionalPostFileds property of type Dictionary
Example 1: Adding parameters
this.radUpload.AdditionalPostFields.Add( "MyParameter", "MyValue" );
Me.radUpload.AdditionalPostFields.Add( "MyParameter", "MyValue" )
File-specific parameters
If you want to pass parameters, that are specific to the file and will be sent with the request for this file only, you have to handle the FileUploadStarting event and add the parameters to its event arguments.
The FileUploadStarting event is fired for each file, when it starts being uploaded.
Example 2: Subscribing to the FileUploadStarting event
<telerik:RadUpload x:Name="radUpload"
FileUploadStarting="radUpload_FileUploadStarting" />
Example 3: Implementing the FileUploadStarting event handler
private void radUpload_FileUploadStarting( object sender, FileUploadStartingEventArgs e )
{
e.FileParameters.Add( "MyFileParameter", "MyFileValue" );
}
Private Sub radUpload_FileUploadStarting(sender As Object, e As FileUploadStartingEventArgs)
e.FileParameters.Add("MyFileParameter", "MyFileValue")
End Sub
The parameters passed through the AdditionalPostFileds property will still be available for each of the uploaded files.
Reading parameters on the server
The parameters passed to the service are stored in the Request.Form property of the RadUploadHandler. They get initialized before the SaveChunkData() is called, so if you want to access them this is the method that has to be overridden.
The SaveChunkData() is a method that gets called every time a portion of the file is transferred. The default size of a chunk is 100 000 bytes and if you want to change the size of the portion, you have to set the BufferSize property of RadUpload. Therefore, if your file is 500 kilobytes, SaveChunkData() will get called 5 times. This is the method that should be used to read or pass parameters, to check the state of the upload via the IsNewFileRequest(), IsFinalFileRequest(), IsFinalUploadRequest() methods.
Example 4: Reading the additional parameters on the server
public override bool SaveChunkData( string filePath, long position, byte[] buffer, int contentLength, out int savedBytes )
{
string myParam = this.GetQueryParameter( "MyParameter" );
return base.SaveChunkData( filePath, position, buffer, contentLength, out savedBytes );
}
Public Overrides Function SaveChunkData(filePath As String, position As Long, buffer As Byte(), contentLength As Integer, savedBytes As Integer) As Boolean
Dim myParam As String = Me.GetQueryParameter("MyParameter")
Return MyBase.SaveChunkData(filePath, position, buffer, contentLength, savedBytes)
End Function
To learn more about the RadUploadHandler and its members read this topic.
Return parameters to the client
You are allowed to return parameters from the service to the client. The desired return parameters should be added via the AddReturnParam(string, object) method. This is done in the override for the GetAssociatedData() method.
Example 5: Adding custom parameters that should be returned to the client
public override Dictionary<string, object> GetAssociatedData()
{
Dictionary<string, object> associatedData = base.GetAssociatedData();
if ( this.IsFinalFileRequest() )
{
associatedData.Add( "MyReturnParam", "MyValue" );
}
return associatedData;
}
Public Overloads Overrides Function GetAssociatedData() As Dictionary(Of String, Object)
Dim associatedData As Dictionary(Of String, Object) = MyBase.GetAssociatedData()
If Me.IsFinalFileRequest() Then
associatedData.Add("MyReturnParam", "MyValue")
End If
Return associatedData
End Function
To access these custom parameters on the client you have to handle the FileUploaded event. They are stored in the HandlerData.CustomData property of the arguments.
Example 6: Subscribing to the FileUploaded event
<telerik:RadUpload x:Name="radUpload"
FileUploaded="radUpload_FileUploaded" />
Example 7: Getting custom parameteres returned from the file upload
private void radUpload_FileUploaded( object sender, FileUploadedEventArgs e )
{
var value = e.HandlerData.CustomData[ "MyReturnParam" ];
}
Private Sub radUpload_FileUploaded(sender As Object, e As FileUploadedEventArgs)
Dim value As var = e.HandlerData.CustomData("MyReturnParam")
End Sub