Jump to content

Recommended Posts

Hi, I am developing in a vb.net silverlight app and posting a file stream to my php script so that I can upload a file within the silverlight app.  Everything works except no data gets sent.  I am new to file streaming through php so not sure what is going wrong.  the result after the upload button is pressed, on the ftp server the file appears in the same directory as the php script but with 0 bytes.  I am using bluehost (linux servers) but the silverlight app is on my desktop and posting to a url, not localhost (shared directory with webpage).. which i'm thinking could be the problem.  Below is the code to both the silverlight and the php.  Please let me know if any of you know whats going on :(

 

Here is my Silverlight Code

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dlg As New OpenFileDialog()
        dlg.Multiselect = False
        dlg.Filter = "All Files | *.*"

        If dlg.ShowDialog() Then
            UploadFile(dlg.File.ToString, dlg.File.OpenRead)
        End If
    End Sub
    Private s As Stream
    Private Sub UploadFile(ByVal fileName As String, ByVal data As Stream)
        Dim ub As New UriBuilder("http://www.mydomain.com/upload.php")
        ub.Query = String.Format("filename={0}", fileName)
        Dim c As New WebClient()
        s = data
        AddHandler c.OpenWriteCompleted, AddressOf c_OpenWriteCompleted
        c.OpenWriteAsync(ub.Uri, "Post")
    End Sub
    Private Sub c_OpenWriteCompleted(ByVal sender As Object, ByVal e As OpenWriteCompletedEventArgs)
        PushData(s, e.Result)
        e.Result.Close()
        s.Close()
    End Sub
    Private Sub PushData(ByVal Input As Stream, ByVal Output As Stream)
        Dim buffer(4096) As Byte
        Dim bytesread As Integer
        While ((bytesread = Input.Read(buffer, 0, buffer.Length)) <> 0)
            Output.Write(buffer, 0, bytesread)
        End While
    End Sub

Here is my PHP code.

$filepath = $_REQUEST['filename'];
$handle = fopen("php://input", "r");
$file = fopen($filepath,"w");

if($file != null && $handle != null)
{
        while ($data = fread($handle, 8192)) fwrite($file, $data);
        fclose($handle);
        fclose($file);
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/180468-solved-file-upload-is-not-fully-working/
Share on other sites

problem with stream code in vb, I found the problem.

 Private Sub PushData(ByVal Output As Stream)
        Dim size As Integer = 4096
        Dim buffer(4096) As Byte
        Dim bytesread As Integer = s.Read(buffer, 0, size)
        While (bytesread > 0)
            Output.Write(buffer, 0, bytesread)
            bytesread = s.Read(buffer, 0, size)
        End While
    End Sub

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.