0

I´m trying to upload .zip files from my VB form to a server, but file loses header information in the process, and I´m unable to find the error/s. Debugging, i found that losed data is from header. Does anyone know why is this happening?

My VB.NET code is:

Imports System.Text

Public Class Form1

    Dim hola As String = "hola"

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        HttpUploadFile("http://localhost/sitio_victor/files.php?parameter1=""value1""&parameter2=" & hola & "", "C:\MDPScan\ParaEnviar\C001_C002_C005_01062016_092619_76CDB2BAD9582D23C1F6F4D868218D6C.zip", "file", "multipart/form-data")

    End Sub

    Private Sub HttpUploadFile( _
    ByVal uri As String, _
    ByVal filePath As String, _
    ByVal fileParameterName As String, _
    ByVal contentType As String)

        Dim process As New Process
        Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
        Dim newLine As String = System.Environment.NewLine
        Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
        request.ContentType = "multipart/form-data; boundary=" & boundary
        request.Method = "POST"
        request.KeepAlive = True
        request.Credentials = Net.CredentialCache.DefaultCredentials
        Using requestStream As IO.Stream = request.GetRequestStream()
            Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}"
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)


            Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3};"
            Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType)
            MsgBox(header)
            Dim headerBytes As Byte() = Encoding.UTF8.GetBytes(header.ToString)
            requestStream.Write(headerBytes, 0, header.Length)

            Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
                Dim buffer(4096) As Byte
                Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
                Do While (bytesRead > 0)
                    requestStream.Write(buffer, 0, bytesRead)
                    bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                Loop

            End Using
            Dim trailer As Byte() = Encoding.ASCII.GetBytes(newLine & "--" + boundary + "--" & newLine)
            requestStream.Write(trailer, 0, trailer.Length)
        End Using

        Dim response As Net.WebResponse = Nothing

        Try
            response = request.GetResponse()
            Using responseStream As IO.Stream = response.GetResponseStream()
                Using responseReader As New IO.StreamReader(responseStream)
                    Dim responseText = responseReader.ReadToEnd()
                    MsgBox(responseText)
                End Using
            End Using
        Catch exception As Net.WebException
            response = exception.Response
            If (response IsNot Nothing) Then
                Using reader As New IO.StreamReader(response.GetResponseStream())
                    Dim responseText = reader.ReadToEnd()
                    Diagnostics.Debug.Write(responseText)
                End Using
                response.Close()
            End If
        Finally
            process.Start(uri)
            request = Nothing
        End Try

    End Sub

End Class

And PHP code is:

<?php


$target_dir = "C:\images";
print_r($_FILES);
//print_r($_POST);
print_r($_REQUEST);

$target_file = $_FILES['file']['tmp_name'];

 if(move_uploaded_file($target_file, $target_dir. DIRECTORY_SEPARATOR . $_FILES['file']['name']))
 {
 echo "Success";
 }
 else
 {
 echo "Failure";
 }

?>

I would be grateful of have any help.

Best regards!

  • Have you tried this? http://stackoverflow.com/questions/981354/how-to-post-a-file-via-http-post-in-vb-net#981402 – Raspberryano Jun 01 '16 at 10:13
  • Yes. If i do it via WebClient it works. But I want to do it this way. I´ve tested all examples I´ve found on internet, but all fails. Maybe i´m doing it wrong, I don´t know. I´m new programming in .NET. –  Jun 01 '16 at 10:23
  • I think that you are doing something like this http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data#2996904 – Raspberryano Jun 01 '16 at 10:27
  • Yes, I tried this code too, but didn´t work for me. Maybe I made a wrong translation.. –  Jun 01 '16 at 10:39
  • What .net framework have you used? You answer me: I´m using .NET Framework 4.5 with Visual Studio 2013 – Raspberryano Jun 01 '16 at 12:09
  • Can you upload a .jpg? I don't know if php part works. It could be a problem about file extensions in php server. (Excuse my english) – Raspberryano Jun 01 '16 at 14:02
  • Don´t worry about your english, i dont speak english well to. A .jpg of code? Or from error? –  Jun 01 '16 at 15:19
  • I say, rather than try a test with a zip, try upload a jpg, maybe a problem in server configuration – Raspberryano Jun 02 '16 at 05:59
  • I tried this before, I,m having same problems. It only sends .txt files.. –  Jun 02 '16 at 07:29
  • Can you post your phpinfo() ? Thanks. Can it be because upload file size? – Raspberryano Jun 02 '16 at 08:34
  • Upload file size is 9M, and the file I´m trying to upload is about 5,7M. Don´t worry dude, I´ll try it via WebClient. I just want to upload files with some extra data. This data will be sent in header, and if it works this way, it´s the fastest way. Thank U for your attention –  Jun 02 '16 at 08:43
  • I'm trying to test your code, I'm installing a virtual machine with a php server. – Raspberryano Jun 02 '16 at 08:46
  • Perfect, tell me if you are able to find the error/s –  Jun 02 '16 at 11:37
  • Ýour php is ok, maybe you must check folder permissions in $target_dir for the user that is running php or iis. Can you say me the values for post_max_size and upload_max_filesize in your php.ini? Thanks – Raspberryano Jun 03 '16 at 09:44
  • This info https://www.sitepoint.com/upload-large-files-in-php/ can be interesting. If I run your vb.net code I get an exception "Se excedió el tiempo de espera de la operación". I think you must read the info in previous url "upload large files in php". I think you must set max_input_time 300 php_value max_execution_time 300 – Raspberryano Jun 03 '16 at 10:01
  • Both post max size and max file size values are 9M. I´ll try to change your last post values –  Jun 03 '16 at 11:04
  • Tell me if it works – Raspberryano Jun 03 '16 at 11:05
  • This doesn't work too –  Jun 03 '16 at 11:35
  • Have you tested with a form in html or with vb.net? – Raspberryano Jun 03 '16 at 11:37
  • No, but finally i did it via webclient, don´t worry. Thanks for your help –  Jun 07 '16 at 13:02
  • De nada. Un saludo. – Raspberryano Jun 08 '16 at 06:29

0 Answers0