Public Sub UploadFile() Dim fileToUpload As String = Me.txtFileToUpload.Text.Trim() Dim fileLength As Long = My.Computer.FileSystem.GetFileInfo(fileToUpload).Length Dim url As String = Me.txtUrl.Text.Trim() Dim port As String = Me.txtPort.Text.Trim() If port <> "" Then Dim u As New Uri(url) Dim host As String = u.Host url = url.Replace(host, host & ":" & port) End If url = url.TrimEnd("/"c) & "/" & IO.Path.GetFileName(fileToUpload) Dim request As HttpWebRequest = _ DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest) request.Method = WebRequestMethods.Http.Put request.ContentLength = fileLength request.SendChunked = True request.Headers.Add("Translate: f") request.AllowWriteStreamBuffering = True Dim s As IO.Stream = Nothing Try 'Send the request to the server, and get the ' server's (file) Stream in return. s = request.GetRequestStream() Catch ex As Exception MessageBox.Show(ex.StackTrace, "An error occurred while attempting to connect to the remote server. " & _ "The following error occurred while attempting to connect:" & vbCrLf & _ vbCrLf & ex.Message) End Try Dim fs As New IO.FileStream(fileToUpload, IO.FileMode.Open, IO.FileAccess.Read) Dim byteTransferRate As Integer = 1024 Dim bytes(byteTransferRate - 1) As Byte Dim bytesRead As Integer = 0 Dim totalBytesRead As Long = 0 pb.Minimum = 0 pb.Maximum = CInt(fileLength \ byteTransferRate) pb.Value = 0 Do 'Read from the file bytesRead = fs.Read(bytes, 0, bytes.Length) If bytesRead > 0 Then totalBytesRead += bytesRead 'Write to stream s.Write(bytes, 0, bytesRead) 'Update progress pb.Value = CInt(totalBytesRead \ byteTransferRate) If pb.Value Mod 500 = 0 Then _ Application.DoEvents() End If Loop While bytesRead > 0 'Close the server stream s.Close() s.Dispose() s = Nothing 'Close the file fs.Close() fs.Dispose() fs = Nothing Dim response As HttpWebResponse = Nothing response = DirectCast(request.GetResponse(), HttpWebResponse) Dim code As HttpStatusCode = response.StatusCode response.Close() response = Nothing If totalBytesRead = fileLength AndAlso _ code = HttpStatusCode.Created Then MessageBox.Show("The file has uploaded successfully!", "Upload Complete", _ MessageBoxButtons.OK, MessageBoxIcon.Information) Else MessageBox.Show("The file did not upload successfully.", _ "Upload failed", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If end sub
Server.MapPath(imageUrl);
HttpContext.Current.Server.MapPath(imageUrl)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)