我正在尝试将.zip文件从我的VB表单上传到服务器,但文件在此过程中丢失了标题信息,我无法找到错误。 调试,我发现丢失的数据来自标头。 有谁知道为什么会这样?
我的VB.NET代码是:
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""¶meter2=" & 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
PHP代码是:
<?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";
}
?>
我很感激有任何帮助。
祝你好运!