本文介绍了如何在ftp服务器上上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码仅复制0字节的文件.请帮助
this code copy only file with 0 bytes. Please Help
Private Sub Upload(ByVal filePath As String, ByVal fileName As String)
ftpServerIP = "192.160.0.10/Employee Documents"
ftpUserID = "h"
ftpPassword = "Har"
Dim reqFTP As FtpWebRequest
Try
'filePath = <<The full path where the file is to be created.>>,
'fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
Dim outputStream As New FileStream(filePath + "\" + fileName, FileMode.Create)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + ftpServerIP + "/" + fileName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.AppendFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
Dim ftpStream As Stream = response.GetResponseStream()
Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
ftpStream.Close()
outputStream.Close()
' MessageBox.Show("Download Complete");
response.Close()
Catch ex As Exception
'MessageBox.Show(ex.Message)
End Try
End Sub
推荐答案
ftpServerIP = "155.166.0.10/Employee Documents"
Dim clsRequest As System.Net.FtpWebRequest = _
DirectCast(FtpWebRequest.Create(New Uri("ftp://" + ftpServerIP + "/" + fileName)), FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("userid", "password")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes(filePath + "\" + fileName)
' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
这篇关于如何在ftp服务器上上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!