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
SQL
public OpenFileDialog fileDialog =null;
fileDialog.Multiselect =false; //one file at a timefileDialog.Filter = ""; //Add file filter
2 - WCF web service to write the file to the server with the following function
Code
[OperationContract]
Code
public void SaveFile(UploadFile UploadFile)
{
FileStream FileStream = new FileStream("[WhateverPathWithPermission]" + UploadFile.FileName, FileMode.Append);
3 - Add to WCF service a class contract to handle segment blocks transfer between client and server.
Code
[DataContract]
Code
public class UploadFile
{
[DataMember]
public string FileName;
Code
[DataMember]
public byte[] File;
}
4 - Handle file blocks server transfer.
Code
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; } }
Code
}
Code
/// function to safe file segment part.
C#
publicvoidsaveFileSegment(int segmentIndex)
{
byte[] bufferSegment = newbyte[15000];
for (int i = 0; i < 15000; i++)
{
bufferSegment[i] = Buffer[15000 * segmentIndex + i];
}
file.File = bufferSegment;
proxy = new MainService.MainServiceClient();
Code
proxy.SaveFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SaveFileCompleted);
proxy.SaveFileAsync(file);
Join the conversation! Your thoughts help the community grow.