Silverlight Upload File

I tried searching for a simple file upload control for silverlight 4 and came across some excellent multi-file upload controls specially the one from codeplex "http://silverlightfileupld.codeplex.com/ ", but that is not what I needed due to complexity or the difficulty of code editing or changing to adjust according to my project .So I began in my personal file uploader as the following,

1 - Open file dialogue to get the file

public OpenFileDialog fileDialog = null;
fileDialog.Multiselect = false; // one file at a time

fileDialog.Filter = ""; // Add file filter


2 - WCF web service to write the file to the server with the following function

[OperationContract]
public void SaveFile(UploadFile UploadFile)
{
FileStream FileStream = new FileStream("[WhateverPathWithPermission]" + UploadFile.FileName, FileMode.Append);
    	FileStream.Write(UploadFile.File, 0, UploadFile.File.Length);
FileStream.Close();
FileStream.Dispose();
}

3 - Add to WCF service a class contract to handle segment blocks transfer between client and server.

[DataContract]
public class UploadFile
{

[DataMember]
public string FileName;
       	[DataMember]
public byte[] File;
}

4 - Handle file blocks server transfer.

 proxy.SaveFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SaveFileCompleted);
if (fileDialog.File != null)
{
Stream strm = fileDialog.File.OpenRead(); // the file dialog you created in step 1
Buffer = new byte[strm.Length];
strm.Read(Buffer, 0, (int)strm.Length);
strm.Dispose();
strm.Close();
file = new MainService.UploadFile(); // use service client proxy
file.FileName = fileDialog.File.Name;
byte[] bufferSegment = new byte[15000];
segments = (Buffer.Length / bufferSegment.Length);
remainder = (Buffer.Length % bufferSegment.Length);
segIndex = 0;
if (Buffer.Length > 15000)
{
saveFileSegment(segIndex);
segIndex++; // for the index of the segment to transfer.
fileIsFinished = false; // to detect if all file transfer segments is finished or not.
}
else
{
file.File = Buffer;
proxy.SaveFileAsync(file, false);
fileIsFinished = true;
}
}
//On complete of segment transfer
void proxy_SaveFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
// check if end of file.
if (fileIsFinished == false)
{
if (segIndex < segments)
{
saveFileSegment(segIndex);
segIndex++;
}
else
{
byte[] bufferSegment = new byte[remainder];
for (int i = 0; i < remainder; i++)
{
bufferSegment[i] = Buffer[15000 * segments + i];
}
file.File = bufferSegment;
proxy = new MainService.MainServiceClient();
proxy.SaveFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SaveFileCompleted);
proxy.SaveFileAsync(file);
fileIsFinished = true;
}
}
}
/// function to safe file segment part.
public void saveFileSegment(int segmentIndex)
{
byte[] bufferSegment = new byte[15000];
for (int i = 0; i < 15000; i++)
{
bufferSegment[i] = Buffer[15000 * segmentIndex + i];
}
file.File = bufferSegment;
proxy = new MainService.MainServiceClient();
proxy.SaveFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SaveFileCompleted);
proxy.SaveFileAsync(file);
}