Uploading with Silveright 2 Beta 1 and ASMX Web Service

By: egoZd (http://www.douziwang.cn/)

This is a guest post by egoZd who created Tetris game with Silverlight 2 (beta1). As some of you might notice that Brad McKelvie wrote an article about how to upload the file with WCF and Silverlight 2 (beta1) in my blog recently. Now, egoZd shares his idea to use ASMX web service instead of WCF for uploading file in Silverlight. I think it would be useful for those who are not familiar with WCF. (Thank you! egoZd.)

If you guys also like to share some tips/tricks, articles or tutorials, please let me know. ~ Michael

SourceCode Download  : SilverLightFileUploadAsmx.zip


  • Create Silverlight project
  • Create Webservice
  • Sending the byte array from Silverlight to ASMX web service

Create Silverlight project

  1. Select File->New->Project choose SilverlightApplication
  2. Visual Studio 2008 will automatically create two project ,Web host of the silverlight and silverlight project.

Create Webservice

You can create the Webservice project separately but in this demo, I will create the webservice in the silverlight hosted web project.

1. Select new item to add a new web servcie.asmx file to the host website project.
2. Add a new webmethod .

public int GetByteInfo(byte[] abyte, string FileExtention)
System.IO.FileStream fs = new System.IO.FileStream(string.Format("C:\\test.{0}", FileExtention),System.IO.FileMode.CreateNew);
fs.Write(abyte, 0, abyte.Length);
return abyte.Length;

Basically, this method is used for receiving the bytes array from Silverlight and will write the file on the server. Note that you should probably change this path to something else that you have the permission to write a file. If you are on Windows Vista, you won’t probably have the permission to write something in C: drive.

Sending the byte array from Silverlight to ASMX web service

1. Add a button control to xaml file

<Button x:Name="btnUplod" Canvas.Left="100" Canvas.Top="130" Content="Please select the file you want to upload" Click="btnUplod_Click" ></Button>

2.Add webservice reference to client silverlight project.

3.Write a upload function for buttion click event.

private void btnUplod_Click(object sender, RoutedEventArgs e)
OpenFileDialog OpenFile = new OpenFileDialog();
if (OpenFile.ShowDialog() == DialogResult.OK)
ServiceReference1.WebServiceSoapClient client = new SilverLightFileUpload.ServiceReference1.WebServiceSoapClient();
client.GetByteInfoCompleted+=new EventHandler<SilverLightFileUpload.ServiceReference1.GetByteInfoCompletedEventArgs>(client_GetByteInfoCompleted);
Stream s = (Stream)OpenFile.SelectedFile.OpenRead();
s.Position = 0;
byte[] buffer = new byte[s.Length + 1];
s.Read(buffer, 0, buffer.Length);
string Extention = OpenFile.SelectedFile.Name.Substring(OpenFile.SelectedFile.Name.IndexOf('.') +1);
client.GetByteInfoAsync(buffer, Extention);
void client_GetByteInfoCompleted(object sender, SilverLightFileUpload.ServiceReference1.GetByteInfoCompletedEventArgs e)


I used a OpenFileDialog to allow the user to select the file. The OpenFileDialog returns the file as a Steam. Then, I converted this steam into a byte array and pass it to web service. My web service will take care of receiving the byte array and create the file on the server. That’s all.

Thanks and Regards,
egoZd (http://www.douziwang.cn/)

2 Replies to “Uploading with Silveright 2 Beta 1 and ASMX Web Service”

  1. Hey,

    Nice tut. Is there a way to send chuncks of bytes to the server, so we could build a progress meter ??



Leave a Reply