Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
VB
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:
VB
<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:
VB
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
Posted
Updated 14-Jun-13 16:07pm
v2
Comments
Please debug and find where exactly it is going wrong.
Idle_Force 15-Jun-13 10:15am    
I have debugged everything except the ashx code - not sure how to. I set a breakpoint in there - never hit.
sid2x 8-Jul-13 6:55am    
Which version of Windows are you using, are you sure your application has privileges to access the internet. Try disabling the Windows firewall. Do you have AVG antivirus, it could be a problem there. Also please specify the message that silverlight shows when the error occurs.
Idle_Force 8-Jul-13 15:56pm    
Windows 7, this is being done via VS developer server and Live from host site does not run any better. Yes I have AVG, I will test it with firewall down...
Idle_Force 8-Jul-13 18:01pm    
No change with firewall down...

1 solution

Try adding a reference to System.IO, and then add this line
Imports System.IO
If you still getting the error then read the comment that I posted...
 
Share this answer
 
Comments
Idle_Force 8-Jul-13 16:06pm    
It is letting me use the classes of System.IO no errors or squiggly lines. Though I cannot reference the dll directly in references.
Sergey Alexandrovich Kryukov 14-Jul-13 0:40am    
You don't have to. This is a false answer, a total bogus. Please disregard it.
—SA
sid2x 9-Jul-13 5:25am    
Okay So you are able to
Run your code. So this means that there are no compiler errors. This means that there is something wrong outside the SDK. But I still don't get what's the problem, the app starts, then does it run until you click the file upload button, or it hangs just like that.
Sergey Alexandrovich Kryukov 14-Jul-13 0:39am    
There is no such assembly as "System.IO", System.IO declarations are in the assembly System.
Totally wrong answer.
—SA
Idle_Force 14-Jul-13 0:59am    
The app does not hang, it just does not upload the file. It seems I need a non-IO solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900