Silverlight doesn't have upload control, here we are uploading a file using OpenFileDialog from silverlight client to server location using wcf.
Step 1.
In Silverlight Web application -> Add -> Add New Item -> Silverlight-enabled wcf -> Add.
Step 2.
In WCF,
[OperationContract]
public void SaveFile(UploadFile uploadfile)
{
FileStream filestream = new FileStream("E:\\Uploaded\\" + uploadfile.FileName, FileMode.Create);
filestream.Write(uploadfile.File, 0, uploadfile.File.Length);
filestream.Close();
filestream.Dispose();
}
FileStream filestream = new FileStream("E:\\Uploaded\\" + uploadfile.FileName, FileMode.Create);
filestream.Write(uploadfile.File, 0, uploadfile.File.Length);
filestream.Close();
filestream.Dispose();
}
[DataContract]
public class UploadFile
{
[DataMember]
public string FileName;
[DataMember]
public byte[] File;
[DataMember]
public string FileName;
[DataMember]
public byte[] File;
}
Step 3.
Add service reference -
Step 4.
//MainPage.xaml
Step 5.
//MainPage.xaml.cs
public OpenFileDialog fileDialog = null;
private void btnUploadFile_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
fileDialog = new OpenFileDialog();
fileDialog.Filter = "All Files|*.*";
bool? result = fileDialog.ShowDialog();
if (result != null && result == true)
{
public MainPage()
{
InitializeComponent();
}
InitializeComponent();
}
private void btnUploadFile_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
fileDialog = new OpenFileDialog();
fileDialog.Filter = "All Files|*.*";
bool? result = fileDialog.ShowDialog();
if (result != null && result == true)
{
Stream strm = fileDialog.File.OpenRead();
byte[] Buffer = new byte[strm.Length]; strm.Read(Buffer, 0, (int)strm.Length);
strm.Dispose();
strm.Close();
//Accessing DataContract...
ServiceReference1.Service1UploadFile file = new ServiceReference1.Service1UploadFile();
file.FileName = fileDialog.File.Name;
file.File = Buffer;
service.SaveFileAsync(file);
service.SaveFileCompleted += new EventHandler <System.ComponentModel.AsyncCompletedEventArgs>(service_SaveFileCompleted);
}
else
{
txtMsg.Text = "Plz Select File !!! ";
}
}
public void service_SaveFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error == null)
txtMsg.Text = fileDialog.File.Name + "Successfully Saved";
}
byte[] Buffer = new byte[strm.Length]; strm.Read(Buffer, 0, (int)strm.Length);
strm.Dispose();
strm.Close();
//Accessing DataContract...
ServiceReference1.Service1UploadFile file = new ServiceReference1.Service1UploadFile();
file.FileName = fileDialog.File.Name;
file.File = Buffer;
service.SaveFileAsync(file);
service.SaveFileCompleted += new EventHandler <System.ComponentModel.AsyncCompletedEventArgs>(service_SaveFileCompleted);
}
else
{
txtMsg.Text = "Plz Select File !!! ";
}
}
public void service_SaveFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error == null)
txtMsg.Text = fileDialog.File.Name + "Successfully Saved";
}
No comments:
Post a Comment