I am working on a file uploader for my Silverlight app In-Browser, but maybe Out-Of-Browser as well. I am not getting any errors, and I can debug the code from WCF RIA and silverlight which seem to be doing the right steps. I am wondering what I am missing in this process. Is there anything else I need to do like web.config? I have done a lot of research and think I am missing something simple. Thanks for looking...
ASHX file:
Imports System.Web
Imports System.Web.Services
Public Class FileUploader : Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Try
Dim str As String
Using streamReader As New IO.StreamReader(context.Request.InputStream)
str = streamReader.ReadToEnd()
Dim array As Byte() = Convert.FromBase64String(str)
Using fileStream As New IO.FileStream(ServerUploadLocation(context), IO.FileMode.Create)
fileStream.Write(array, 0, array.Length)
End Using
End Using
Catch
End Try
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Private Function ServerUploadLocation(context As HttpContext) As String
Dim fileName = context.Request.QueryString("filename")
Dim server As String = context.Request.QueryString("server")
Return String.Format("{0}/{1}", server, fileName)
End Function
End Class
WCF:
<OperationContract()>
Public Sub UploadMyFile(file() As Byte, filename As String)
Dim wb As New WebClient
AddHandler wb.OpenWriteCompleted, AddressOf UploadOpenWriteComplete
wb.OpenWriteAsync(New Uri(filename), Nothing, file)
End Sub
Private Sub UploadOpenWriteComplete(sender As Object, e As Net.OpenWriteCompletedEventArgs)
If e.Error Is Nothing Then
Dim bytes() As Byte = TryCast(e.UserState, Byte())
Dim outputStream As IO.Stream = e.Result
outputStream.Write(bytes, 0, bytes.Length)
outputStream.Close()
End If
End Sub
Silverlight:
Dim server As String = tbServer.Text.Trim
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = True Then
Dim strm As IO.Stream = ofd.File.OpenRead
Dim fileSize As Integer = CInt(strm.Length)
Dim buffer(fileSize) As Byte
strm.Read(buffer, 0, fileSize)
Dim s As String = ServerAddr(ofd.File.Name, server)
AddHandler serv.UploadMyFileCompleted, AddressOf FileUploadCompleted
serv.UploadMyFileAsync(buffer, s)
End If
Private Function ServerAddr(filename As String, server As String) As String
Return String.Format("http://{domain name}/FileUploader.ashx/?filename={0}&server={1}", filename, server)
End Function