Step 1) Create Silverlight Project with .Web Option
i.e SilverlightApplication and SilverlightApplication.Web
Step 2) Add WCF service to SilverlightApplication.Web
Interface
<ServiceContract> _
Public Interface IService1
<OperationContract> _
Boolean uploads(byte() bytes, String fileName)
End Interface
Implementation
Public Class Service1
Implements IService1
Public Function uploads(ByVal bytes() As Byte, ByVal fileName As String) As Boolean
Dim b As Boolean = Not True
Try
fileName = "C:\inetpub\wwwroot\SilverlightApplication.Web\Uploads\"+fileName
Dim ms As MemoryStream = New MemoryStream(bytes)
Dim reader As StreamReader = New StreamReader(ms)
System.IO.File.WriteAllText(fileName,reader.ReadToEnd())
b = True
Catch ex As Exception
Throw ex
End Try
Return b
End Function
End Class
make sure uploads folder has write permissions
Leave Web.config settings, because .net 4.0 will take care of it.
Step 3) Run the Service.
in my App:it is http://localhost:3205/Service1.svc
Step 4) Add Service Reference to SilverlightApplication Project, it will create a proxy class.
Step5) in the XAML Add button and Label to grid layout
<Grid x:Name="LayoutRoot" Height="378">
<TextBlock Text="File Uploading in Silverlight using WCF Service" FontSize="16" Foreground="Red" Margin="10,10,-10,332"
>
</TextBlock>
<Button Content="Upload File(s)" HorizontalAlignment="Left" VerticalAlignment="Top" Width="159" x:Name="fileuploadone" Margin="192,106,0,0" Click="fileuploadone_Click" Height="27"/>
<sdk:Label Content="no uploaded files" x:Name="lblfilestats" HorizontalAlignment="Left" Height="123" VerticalAlignment="Top" Width="454" Margin="58,176,0,0"/>
</Grid>
Button handler invokes dialog box to select a file(s).
Label will display Filename uploaded to server using WCF Service.
Step 6) Invoke upload WCF service client in Button Handler.
Private Sub fileuploadone_Click(sender As Object, e As RoutedEventArgs)
Try
Dim od As New OpenFileDialog()
od.Multiselect = True
Dim b As System.Nullable(Of Boolean) = od.ShowDialog().GetValueOrDefault(True)
If b.HasValue Then
If b.Value = True Then
Dim client As New ServiceReference1.Service1Client()
For Each fi As FileInfo In od.Files
lblfilestats.Content = fi.Name + fi.Length + fi.Extension
Dim fs As FileStream = fi.OpenRead()
Dim bytes As Byte() = New Byte(fs.Length - 1) {}
client.uploadsAsync(bytes, fi.Name)
client.uploadsCompleted += client_uploadsCompleted
Next
Else
MessageBox.Show("showdialog returns false")
' od.Files
End If
Else
MessageBox.Show("showdialog returns no value")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub client_uploadsCompleted(sender As Object, e As ServiceReference1.uploadsCompletedEventArgs)
If e.[Error] IsNot Nothing Then
MessageBox.Show(e.[Error].Message)
Return
End If
If e.Result Then
lblupload.Content = "Success"
Else
lblupload.Content = "Failed"
End If
End Sub
Step 7) Check Uploads Folder for uploaded files
i.e SilverlightApplication and SilverlightApplication.Web
Step 2) Add WCF service to SilverlightApplication.Web
Interface
<ServiceContract> _
Public Interface IService1
<OperationContract> _
Boolean uploads(byte() bytes, String fileName)
End Interface
Implementation
Public Class Service1
Implements IService1
Public Function uploads(ByVal bytes() As Byte, ByVal fileName As String) As Boolean
Dim b As Boolean = Not True
Try
fileName = "C:\inetpub\wwwroot\SilverlightApplication.Web\Uploads\"+fileName
Dim ms As MemoryStream = New MemoryStream(bytes)
Dim reader As StreamReader = New StreamReader(ms)
System.IO.File.WriteAllText(fileName,reader.ReadToEnd())
b = True
Catch ex As Exception
Throw ex
End Try
Return b
End Function
End Class
make sure uploads folder has write permissions
Leave Web.config settings, because .net 4.0 will take care of it.
Step 3) Run the Service.
in my App:it is http://localhost:3205/Service1.svc
Step 4) Add Service Reference to SilverlightApplication Project, it will create a proxy class.
Step5) in the XAML Add button and Label to grid layout
<Grid x:Name="LayoutRoot" Height="378">
<TextBlock Text="File Uploading in Silverlight using WCF Service" FontSize="16" Foreground="Red" Margin="10,10,-10,332"
>
</TextBlock>
<Button Content="Upload File(s)" HorizontalAlignment="Left" VerticalAlignment="Top" Width="159" x:Name="fileuploadone" Margin="192,106,0,0" Click="fileuploadone_Click" Height="27"/>
<sdk:Label Content="no uploaded files" x:Name="lblfilestats" HorizontalAlignment="Left" Height="123" VerticalAlignment="Top" Width="454" Margin="58,176,0,0"/>
</Grid>
Button handler invokes dialog box to select a file(s).
Label will display Filename uploaded to server using WCF Service.
Step 6) Invoke upload WCF service client in Button Handler.
Private Sub fileuploadone_Click(sender As Object, e As RoutedEventArgs)
Try
Dim od As New OpenFileDialog()
od.Multiselect = True
Dim b As System.Nullable(Of Boolean) = od.ShowDialog().GetValueOrDefault(True)
If b.HasValue Then
If b.Value = True Then
Dim client As New ServiceReference1.Service1Client()
For Each fi As FileInfo In od.Files
lblfilestats.Content = fi.Name + fi.Length + fi.Extension
Dim fs As FileStream = fi.OpenRead()
Dim bytes As Byte() = New Byte(fs.Length - 1) {}
client.uploadsAsync(bytes, fi.Name)
client.uploadsCompleted += client_uploadsCompleted
Next
Else
MessageBox.Show("showdialog returns false")
' od.Files
End If
Else
MessageBox.Show("showdialog returns no value")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub client_uploadsCompleted(sender As Object, e As ServiceReference1.uploadsCompletedEventArgs)
If e.[Error] IsNot Nothing Then
MessageBox.Show(e.[Error].Message)
Return
End If
If e.Result Then
lblupload.Content = "Success"
Else
lblupload.Content = "Failed"
End If
End Sub
Step 7) Check Uploads Folder for uploaded files


No comments:
Post a Comment