i will teach you how to upload file and passing parameter at the same time from vb.net to PHP web page. it's not using FTP protocol but use standart protocol (80). don't worry about difficulty i will explain to you.
HowTo
1. First you must prepare webserver (apache) and turn it on. i won't explain how to install apache.
2. write the php code for upload and give name for example upload.php and create folder give name uploads. write php script for upload :
$target_dir = "uploads/"; print_r($_FILES); //print_r($_POST); print_r($_REQUEST); //echo $_POST["nama"]; $target_file = $_FILES['fileToUpload']['tmp_name']; if(move_uploaded_file($target_file, $target_dir.'filesukses')) { echo "Success"; } else { echo "Failure"; }3. open vb.net create one project , add one button on form and at click events write this code :
HttpUploadFile("http://localhost/upload/upload.php?parameter1=""value1""¶meter2=""value2""", "C:\myfileupload.txt", "fileToUpload", "multipart/form-data")4. add import :
Imports System.IO Imports System.Net Imports System.Text5. add private function
Private Sub HttpUploadFile( _ ByVal uri As String, _ ByVal filePath As String, _ ByVal fileParameterName As String, _ ByVal contentType As String) 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) 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 request = Nothing End Try End Sub6. click the button and you will see myfileupload.txt already uploaded to webserver and the webserver give some response in text. the response is print_r from array $_FILES and print_r from array $_request.
simple right ? :)
Post a Comment
Harap gunakan bahasa yang baik dan sopan, terima kasih