09-06-14, 10:25 AM
.....
منقول مع تعديل بسيط ليناسب طلبك
VB.NET
طريقة استخدام للدالتين WebClient وHttpWebRequest
' باستخدام WebClient
' باستخدام HttpWebRequest
ملف uploadfile.php
.....
منقول مع تعديل بسيط ليناسب طلبك
VB.NET
طريقة استخدام للدالتين WebClient وHttpWebRequest
كود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using op As New OpenFileDialog
If op.ShowDialog = DialogResult.OK Then
Dim filename As String = op.FileName
Dim url As String = "http://xxxxxxxxxx.xxx/uploadfile.php"
Dim result As String = uploadfile(url, filename)
MsgBox(result)
End If
End Using
End Sub' باستخدام WebClient
كود :
Private Function uploadfile(ByVal url As String, ByVal filename As String) As String
Try
Dim result As String
Using wc = New Net.WebClient()
Dim responseArray As Byte() = wc.UploadFile(url, filename)
result = System.Text.Encoding.UTF8.GetString(responseArray)
End Using
Return result
Catch ex As Exception
Return ex.Message
End Try
End Function' باستخدام HttpWebRequest
كود :
Private Function uploadfile(ByVal url As String, ByVal filename As String) As String
Try
Dim boundary As String = IO.Path.GetTempFileName
Dim header As New System.Text.StringBuilder()
header.AppendLine("--" & boundary)
header.Append("Content-Disposition: form-data; name=""file"";")
header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filename))
header.AppendLine()
header.AppendLine("Content-Type: application/octet-stream")
header.AppendLine()
Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
Dim endboundarybytes() As Byte = System.Text.Encoding.UTF8.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
request.ContentType = "multipart/form-data; boundary=" & boundary
request.ContentLength = headerbytes.Length + New IO.FileInfo(filename).Length + endboundarybytes.Length
request.Method = "POST"
Using stream As IO.Stream = request.GetRequestStream
Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filename)
stream.Write(headerbytes, 0, headerbytes.Length)
stream.Write(filebytes, 0, filebytes.Length)
stream.Write(endboundarybytes, 0, endboundarybytes.Length)
stream.Close()
End Using
Dim result As String
Using response As Net.WebResponse = request.GetResponse
Using stream As IO.Stream = response.GetResponseStream
Using reader As New IO.StreamReader(stream)
result = reader.ReadToEnd
reader.Close()
End Using
stream.Close()
End Using
End Using
Return result
Catch ex As Exception
Return ex.Message
End Try
End Functionملف uploadfile.php
كود :
<?php
try {
$dir = "files"; // المجلد الذي يتم الحفظ فيه
// التأكد من وجود المجلد
if ( ! is_dir ($dir)) {
mkdir($dir); // إنشاء المجلد
}
// الحفظ في المجلد المحدد
if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir . "/" . $_FILES["file"]["name"])){
echo "تم رفع الملف بنجاح";
} else {
echo "فشلت عملية رفع الملف";
}
} catch (Exception $e) {
die ('لم يتم رفع الملف للأسباب التالية: ' . $e->getMessage());
}
?>.....



