Solved

Uploading a file to a perl script in VB.NET using POST

Posted on 2004-10-11
3
206 Views
Last Modified: 2012-06-21
I have the folloing HTML form that works pefectly well uploading files to a perl script

<html><B>File Uploader</B><p></p>
<form  name='frm' enctype='multipart/form-data' action='https://www.myurl.com/cgi-bin/upload.pl?p1=111&p2=adestindationfilename.jpg&p3=xxxx&cmd=PUT' method='POST'>    
<input name='lclfile' type='file' value=''>
<input name='Submit' type='submit' value='Upload'>
 </form>      
</html>


I want to duplicate the HTML functionality above in a .NET assembly (fully trusted) so that the file selection and the POST occurs automatically. The file is ALWAYS a binary image and the parameters to the perl script are mandatory.

I tried to following code to simulate the Submit on a normal SSL Web form which fails to upload. Any ideas why?
The error occurs at System.NET.ConnectStream.get_Length() - Not supported Exception
PS. I have control of the perl script too.

                  pURL = "https://www.myurl.com/cgi-bin/upload.pl"

                    'Store any credential cookies in container
                    'Create the Web Request
                    strParamData = "p1=111&p2=adestinationfilename.jpg&p3=xxxx&cmd=PUT"
                    Dim PUTRequest As HttpWebRequest = HttpWebRequest.Create(pURL & "?" & strParamData)
                    'Give default credentials to the PutRequest - 'normaly this would be from a CredentailCache
                    PUTRequest.Credentials = CredentialCache.DefaultCredentials
                   
                    Dim myBoundary As String = "----------" + DateTime.Now.Ticks.ToString("x")
                    PUTRequest.ContentType = "multipart/form-data; boundary=" & myBoundary
                    PUTRequest.Method = "POST"

                    'Now Setup the POST Message
                    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
                    sb.Append("--")
                    sb.Append(myBoundary)
                    sb.Append("\r\n")
                    sb.Append("Content-Disposition: " & "form-data; name=""lclfile""" & "; filename=""")
                    sb.Append(_mylocalfile)
                    sb.Append("""")
                    sb.Append("\r\n")
                    sb.Append("Content-Type: application/octet-stream")
                    sb.Append("\r\n")
                    sb.Append("\r\n")

                    'Put the contents of the sting into a header
                    Dim myHeader As String = sb.ToString
           
                    Dim myHeaderBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(myHeader)

                    'Now build the tailing boundary
                    Dim myBoundaryBytes As Byte() = System.Text.Encoding.UTF8.GetBytes("\r\n" & myBoundary & "\r\n")

                    'Get the file into a Stream
                    Try
                        If System.IO.File.Exists(_mylocalfile) Then
   
                            Dim myFileStream As New FileStream(_mylocalfile FileMode.Open, FileAccess.Read)
                            Dim myFileLen As Integer
                            myFileLen = myFileStream.Length
                            Dim filebuffer(myFileLen) As Byte

                            myFileStream.Read(filebuffer, 0, filebuffer.Length)
                            myFileStream.Close()



                            'Total Length of POST
                            Dim myTotalPOSTLength As Long = myHeaderBytes.Length + filebuffer.Length + myBoundaryBytes.Length
                            PUTRequest.ContentLength = myTotalPOSTLength
                            Dim myReqStream As Stream = PUTRequest.GetRequestStream

                            myReqStream.Write(myHeaderBytes, 0, myHeaderBytes.Length)
                            'Now write out the file contents
                            myReqStream.Write(filebuffer, 0, filebuffer.Length)
                            'Now write out the tailing boundary
                            myReqStream.Write(myBoundaryBytes, 0, myBoundaryBytes.Length)
                            'Now we get the Response back
                            Dim myResponse As WebResponse = PUTRequest.GetResponse
                        End If

                    Catch ex As Exception
                        MsgBox("Error Occurred: " & ex.Message)
                    End Try




0
Comment
Question by:westerdale
3 Comments
 
LVL 1

Author Comment

by:westerdale
ID: 12347656
Note to Administrator: I've solved this issue myself...Can I give myself 500 points <g>

just  PUTRequest.AllowWriteStreamBuffering = True was missing!

Regards,

Simon
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12663305
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question