3

I'm trying to upload multiple files with VB.Net code but have had no success. I end up with only one file on the webserver when there is more than one file (FileExists is True for multiple elements). If I finish the loop in other some place, it raises an exception.

This is my code:

Imports System.Net
Imports System.IO

Sub upload()
    Dim filepath As String = IO.Path.Combine(Application.StartupPath, "C:\i.bmp")
    Dim filepath2 As String = IO.Path.Combine(Application.StartupPath, "C:\i.txt")
    Dim url As String = "http://localhost/SEND/MultUp.php"

    Dim Element As Object
    Dim sNames(0 To 3) As String 'Array declaration

    sNames(0) = filepath2
    sNames(1) = ""
    sNames(2) = ""
    sNames(3) = filepath

    Dim boundary As String = IO.Path.GetRandomFileName
    Dim header As New System.Text.StringBuilder()

    For Each Element In sNames 'Roaming all elements of array
        If File.Exists(Element) Then
            header.AppendLine("--" & boundary)
            header.Append("Content-Disposition: form-data; name=""files[]"";")
            header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(Element))
            header.AppendLine()
            header.AppendLine("Content-Type: application/octet-stream")
            header.AppendLine()

            Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
            Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)

            Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
            req.ContentType = "multipart/form-data; boundary=" & boundary
            req.ContentLength = headerbytes.Length + New IO.FileInfo(Element).Length + endboundarybytes.Length
            req.Method = "POST"

            Dim s As IO.Stream = req.GetRequestStream
            s.Write(headerbytes, 0, headerbytes.Length)
            Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(Element)
            s.Write(filebytes, 0, filebytes.Length)
            s.Write(endboundarybytes, 0, endboundarybytes.Length)
            s.Close()

            header.clear() 'this was missing in my original source code
        End If
    Next
End Sub
Zairja
  • 1,441
  • 12
  • 31
  • 6
    You aren't emptying your StringBuilder as far as I can see, so that may be an issue. – jmcilhinney Feb 27 '14 at 02:35
  • Very good your suggestion @jmcilhinney! Adding => header.Clear() before => End If works fine now. Thank you very much. –  Feb 27 '14 at 03:22
  • on the server side, have you got a loop set up to go through th multiple Form1.Requests? – Frank_Vr Mar 20 '14 at 14:21
  • Yes @Frank_Vr! I have a php script with a correct configuration for this make by myself. –  Mar 22 '14 at 21:33

1 Answers1

0

Full credit to jmcilhinney's comment:

The originally posted code was missing the header.clear() call. Therefore, as multiple elements were processed, the header variable continued to append lines.

A better practice would be to declare the header StringBuilder variable within the For Each loop. It makes it easier to maintain, and the compiler should optimize it either way. Since header isn't used outside the loop, it should be used in the narrowest scope.

Community
  • 1
  • 1
Zairja
  • 1,441
  • 12
  • 31