Solved

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

Posted on 2004-10-11
3
201 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now