We help IT Professionals succeed at work.

WCF Rest Image Upload

SniperX1 used Ask the Experts™

I am working on a simple WCF Rest Service, that will be used by iPhone to upload images to a database.

The problem I am getting is with the Stream parameter when I run the service in visual studio, which opens up a web page, attachment shows the error.

This is the Service Contract Interface

<ServiceContract()> _
Public Interface IRestful

    <OperationContract()> _
    <WebInvoke(Method:="POST", UriTemplate:="UploadImage/{filename}")> _
    Sub UploadImage(ByVal fileName As String, ByVal fileContents As Stream)

End Interface

and the Implementation

Public Class Restful
    Implements IRestful

    Public Sub UploadImage(ByVal fileName As String, ByVal fileContents As Stream) Implements IRestful.UploadImage

        Dim buffer As Byte()
        ReDim buffer(32767)
        Dim ms As New MemoryStream()
        Dim bytesRead As Integer, totalBytesRead As Integer = 0

            bytesRead = fileContents.Read(buffer, 0, buffer.Length)
            totalBytesRead += bytesRead

            ms.Write(buffer, 0, bytesRead)

        Loop While bytesRead > 0

        Dim data As New DataAccess(fileName, ms.ToArray())

        Catch ex As Exception
            Console.WriteLine("Upload failed - {0}", ex.ToString)
        End Try

        Console.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead)

    End Sub

End Class

And this is the client code I will use, for development purposes of the service before I even start with the IPhone part.

    Public Sub DoUpload()


            Dim url As String = "http://localhost:1447/Restful.svc"
            Dim requestUrl As String = String.Format("{0}/UploadFile/{1}", url, System.IO.Path.GetFileName("c:\Lordsevenbigger.jpg"))

            Dim request As Net.HttpWebRequest = CType(HttpWebRequest.Create(requestUrl), HttpWebRequest)
            request.Method = "POST"
            request.ContentType = "text/plain"

            Dim fileToSend As Byte() = File.ReadAllBytes("c:\Lordsevenbigger.jpg")
            request.ContentLength = fileToSend.Length

            Using requestStream As Stream = request.GetRequestStream()
                requestStream.Write(fileToSend, 0, fileToSend.Length)
            End Using

            MessageBox.Show("File sucessfully uploaded.", "Upload", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            MessageBox.Show("Error during file upload: " + ex.Message, "Upload", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

Am I also correct in saying a WCF service or REST type is best for when I want the service to be consumed by, IPhone, Android, asp.net website etc.

Thank you for any help in advance it is greatly appreciated.

Kind Regards

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

What is your question?

(All that code, and it comes down to an attachment that I missed)

Can you attach the stack trace for the message, instead of .bmp file, please?
The problem is with WSDL, as the service is Rest when I try to view it in browser, WSDL cannot handle the extra Stream Parameter, even though the method works fine when called from a client app.


I found out the answer