RadControls for Silverlight

This tutorial will show you how to personalize the storage folder, so each user who uses your application can upload the files in his own folder.

Tip
To see an example of how to implement simple authentication read here.

In order to achieve the personalization of the storage you have to do the following things:

Prepare the Account Session

In order to prepare the account session you have to create a method that gets the account name and determines whether the user is authorized.

Note

The account name and the authorization information should be passed as parameters to the Upload Handler. To learn how to pass parameters to it read this topic.

CopyC#
bool authorized = false;
string accountName = string.Empty;
private void PrepareAccountSession()
{
    string dataAuthorized = this.GetQueryParameter( "Authorized" );
    this.authorized = !string.IsNullOrEmpty( dataAuthorized )
        && dataAuthorized.ToLower() == "true";
    this.accountName = this.GetQueryParameter("Account");
    if ( !this.authorized || string.IsNullOrEmpty( this.accountName ) )
        this.accountName = "Anonymous";
}
CopyVB.NET
Private authorized As Boolean = False
Private accountName As String = String.Empty
Private Sub PrepareAccountSession()
 Dim dataAuthorized As String = Me.GetQueryParameter("Authorized")
 Me.authorized = Not String.IsNullOrEmpty(dataAuthorized) AndAlso dataAuthorized.ToLower() = "true"
 Me.accountName = Me.GetQueryParameter("Account")
 If Not Me.authorized OrElse String.IsNullOrEmpty(Me.accountName) Then
  Me.accountName = "Anonymous"
 End If
End Sub

Override the GetTargetFolder method

The storage folder should be inside the Target Folder and must be named after the user. To do that you have to override the GetTargteFolder() method.

CopyC#
public override string GetTargetFolder()
{
    string path = base.GetTargetFolder();
    if ( !string.IsNullOrEmpty( this.accountName ) )
    {
        path += Path.DirectorySeparatorChar + this.accountName;
    }
    return path;
}
CopyVB.NET
Public Overloads Overrides Function GetTargetFolder() As String
 Dim path As String = MyBase.GetTargetFolder()
 If Not String.IsNullOrEmpty(Me.accountName) Then
  path += Path.DirectorySeparatorChar + Me.accountName
 End If
 Return path
End Function

Prepare the Storage Folder

In order to prepare the storage folder you have to create a method that checks whether the folder returned by the GetTargetFolder() method exists and creates the folder if needed.

CopyC#
private bool PrepareStorageFolder()
{
    string folderPath = this.GetTargetFolder();
    if ( !Directory.Exists( folderPath ) )
    {
        Directory.CreateDirectory( folderPath );
    }
    return Directory.Exists( folderPath );
}
CopyVB.NET
Private Function PrepareStorageFolder() As Boolean
 Dim folderPath As String = Me.GetTargetFolder()
 If Not Directory.Exists(folderPath) Then
  Directory.CreateDirectory(folderPath)
 End If
 Return Directory.Exists(folderPath)
End Function

Override the SaveChunkData method

Overriding the SaveChunkData() method allows you to put all this together.

CopyC#
public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
{
 bool result;
 this.PrepareAccountSession();
 if (!this.PrepareStorageFolder())
 {
  result = false;
 }

 string FilePath = this.GetFilePath();
 result = base.SaveChunkData(FilePath, position, buffer, contentLength, out savedBytes);
 return result;
}
CopyVB.NET
Public Overrides Function SaveChunkData(filePath__1 As String, position As Long, buffer As Byte(), contentLength As Integer, ByRef savedBytes As Integer) As Boolean
    Dim result As Boolean
    Me.PrepareAccountSession()

    If Not Me.PrepareStorageFolder() Then
        result = False
    End If

    Dim FilePath__2 As String = Me.GetFilePath()
    result = MyBase.SaveChunkData(FilePath__2, position, buffer, contentLength, savedBytes)

    Return result
End Function

See Also