Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello!


I'm trying to upload a file to a self-made website using php.
I can upload a file using a html file on the server but I wanted to do it with VB.net app.

This is what I already found (and edited) on the net.

When I want to run this code, I receive an error in this line:
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

Normally the last 10 lines of code works ok when I wanted to upload normal data (e.g. a name or other string,...).

What's wrong? My header? Or the way I want to write it to the stream, ...?


VB
Function SendFile(ByVal filepath As String, ByVal feedbackUrl As String)
       Dim boundery As String = IO.Path.GetRandomFileName
       Dim header As New System.Text.StringBuilder()
       header.AppendLine("--" & boundery)
       header.Append("Content-Disposition: form-data; name=""userfile"";")
       header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filepath))
       header.AppendLine()
       header.AppendLine("Content-Type: application/octet-stream")
       header.AppendLine()

       'Convert in binary format (ASCII)
       Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
       Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundery & "--" & vbNewLine)

       'Create and initialize the request
       Dim request As HttpWebRequest = DirectCast(WebRequest.Create(feedbackUrl), HttpWebRequest)
       request.UserAgent = "My Program Agent"
       request.Method = "POST"

       'Write POST data to request
       request.ContentType = "multipart/form-data; boundary=" & boundery
       request.ContentLength = headerbytes.Length + New FileInfo(filepath).Length + endboundarybytes.Length
       Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)
       Dim requestStream As System.IO.Stream = request.GetRequestStream()
       requestStream.Write(headerbytes, 0, headerbytes.Length)
       requestStream.Write(filebytes, 0, filebytes.Length)
       requestStream.Write(endboundarybytes, 0, endboundarybytes.Length)
       requestStream.Close()

       'Execute request and read response.
       Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
       Dim streamgt As System.IO.Stream = response.GetResponseStream()
       Dim length As Int64 = 100000
       Dim txt(length) As Byte
       streamgt.Read(txt, 0, length)
       streamgt.Close()
       streamgt.Dispose()
       Return txt
   End Function
Posted
Updated 19-Feb-11 10:04am
v2
Comments
Sandeep Mewara 20-Feb-11 0:01am    
Can you add on, what error?
wimvr 20-Feb-11 3:45am    
This is the error I got:

The underlying connection was closed: An unexpected error occurred during the reception.

1 solution

My initial reaction is that your boundary sting may contain illegal characters.

Try setting your boundary to literal string like "1234567890-0987654321" and try again. That may resolve it, if it does look into another method of getting a random boundary.
 
Share this answer
 

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