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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集