这是我痛苦的问题。我正在从一个FTP(一个开发站点)转移到另一个FTP(一个测试站点)。让我想起改变这个过程的想法。它不在我手中。无论如何,这是我的方法:
Public Function TransferFile(originalFile As String, destinationFile As String) As String
Try
'FileStream for holding the file
Dim uploadRequest As FtpWebRequest = WebRequest.Create(destinationFile)
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
uploadRequest.Credentials = New NetworkCredential(ftp_user, ftp_pw)
uploadRequest.UseBinary = True
uploadRequest.UsePassive = False
'connect to the server
Dim fileRequest As FtpWebRequest = WebRequest.Create(originalFile)
fileRequest.Method = WebRequestMethods.Ftp.DownloadFile
fileRequest.Credentials = New NetworkCredential(ftp_user, ftp_pw)
fileRequest.UseBinary = True
fileRequest.UsePassive = False
'get the servers response
Dim response As WebResponse = fileRequest.GetResponse()
'retrieve the response stream
Dim stream As Stream = response.GetResponseStream()
CopyStream(stream, uploadRequest.GetRequestStream)
stream.Close()
response.Close()
Return "File transfered"
Catch ex As System.Security.SecurityException
Return ex.Message
Catch ex As Exception
Return ex.Message
End Try
End Function
Public Shared Sub CopyStream(input As Stream, output As Stream)
Dim buffer As Byte() = New Byte(32767) {}
While True
Dim read As Integer = input.Read(buffer, 0, buffer.Length)
If read <= 0 Then
Return
End If
output.Write(buffer, 0, read)
End While
End Sub
这适用于ASPX文件及其.vb代码后台。当我们尝试传输.DLL文件时,它们在服务器上显示为0字节,有时实际上是传输。问题在于,尽管尺寸与原件尺寸相同,但它们表现得好像腐败一样。有没有人有解决方案?
答案 0 :(得分:0)
只是猜测 - 在子定义中使用BYREF
Public Shared Sub CopyStream(BYREF输入为Stream,BYREF输出为Stream)
答案 1 :(得分:0)
关闭输出流并从uploadRequest获取响应。