dragon0023 2016-06-01 10:00
浏览 67

尝试将.zip文件从VB .NET上传到服务器时出错

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler