16,721
社区成员




Try
Dim myWebClient As New WebClient
myWebClient.UploadFile("http://192.168.1.201/2/1.zip", "put", "E:\psp\1\1.zip")
Catch ex As Exception
End Try
Try
Dim myWebClient As New WebClient
Dim fs As FileStream = New FileStream("E:\psp\电影\rleis.zip", FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim uriString As Uri = New Uri("http://192.168.1.201/zdimp/rleis.zip")
Dim postArray As Byte() = br.ReadBytes(CInt(fs.Length))
Dim postStream As Stream = myWebClient.OpenWrite(uriString, "PUT")
If postStream.CanWrite Then
postStream.Write(postArray, 0, postArray.Length)
End If
postStream.Close()
Catch ex As Exception
MsgBox(ex.ToString.Trim)
End Try
Private Function uploadFile() As Boolean
Try
Me.lblInfo.Text = "正在上传文件: " & strFileName & " ,请稍后..."
Dim DownLaodDelegate As New DownLaodTextDg(AddressOf DownLoadText)
Dim uploadUrl As New System.Uri(strServerPath & strFileName)
Dim hwr As System.Net.HttpWebRequest = System.Net.WebRequest.Create(uploadUrl)
Dim fs As New System.IO.FileStream(strLocalPath, FileMode.Open, FileAccess.Read) '得到本地文件的数据流
Dim totalBytes As Long = fs.Length
Dim percent As Integer = 0
hwr.Timeout = 1000 * 5 * 60 '设置超时值30秒
hwr.Method = "PUT" '传送方式 这里一定要是PUT才行
hwr.UseDefaultCredentials = True
hwr.KeepAlive = True
hwr.ContentLength = totalBytes '设置传送的数据长度
Me.Invoke(DownLaodDelegate, totalBytes, 0, 0, 0, False)
Dim PostStream As Stream = hwr.GetRequestStream() '发送请求
Dim totalUploadByte As Long = 0
Dim by(1024) As Byte
Dim osize As Integer = fs.Read(by, 0, CInt(by.Length))
'向服务器写入文件
Do While osize > 0
totalUploadByte = osize + totalUploadByte
PostStream.Write(by, 0, osize)
osize = fs.Read(by, 0, CInt(by.Length))
percent = (totalUploadByte / totalBytes) * 100
Me.Invoke(DownLaodDelegate, totalBytes, CInt(totalUploadByte), percent, 0, False)
Loop
fs.Close()
PostStream.Close()
PostStream.Dispose()
Me.Invoke(DownLaodDelegate, totalBytes, CInt(totalUploadByte), 100, 0, True)
Dim response As System.Net.HttpWebResponse = CType(hwr.GetResponse(), System.Net.HttpWebResponse)
response.Close()
Dim strStatusCode As String = response.StatusCode.ToString.Trim.ToUpper
If strStatusCode = "CREATED" Or strStatusCode = "OK" Then
Return True
End If
Return False
Catch ex As Exception
MsgBox(ex.ToString.Trim, MsgBoxStyle.Exclamation, "系统提示")
Return False
End Try
End Function