我有一个简单的VisualBasic项目,来自FTP服务器的downlad文件。所以这是执行此操作的代码:
Public Sub TestMethod1()
Dim fileList As New Generic.List(Of String)
'GET FILE FROM SERVER
Dim objFTPRequest As FtpWebRequest = CType(FtpWebRequest.Create(New Uri("ftp://ftp.address.it")), FtpWebRequest)
objFTPRequest.Credentials = New NetworkCredential("user", "pass")
objFTPRequest.KeepAlive = False
objFTPRequest.UsePassive = False
objFTPRequest.UseBinary = True
objFTPRequest.Method = WebRequestMethods.Ftp.ListDirectory
Dim response As FtpWebResponse = CType(objFTPRequest.GetResponse(), FtpWebResponse)
Using respStream As StreamReader = New StreamReader(objFTPRequest.GetResponse().GetResponseStream())
Dim line As String = respStream.ReadLine()
While line IsNot Nothing
If Regex.IsMatch(line, "^.*\.(pdf|doc|txt|zip|xlsx)$", RegexOptions.IgnoreCase) Then
'If line Like _config.FileSearch Then
fileList.Add(line)
End If
line = respStream.ReadLine()
End While
End Using
'DOWNLOAD FILE INTO FILELIST
For Each fileName As String In fileList
DownloadFile(fileName)
Next
End Sub
Public Function DownloadFile(ByVal pFileName As String) As String
Dim strPath As String = String.Empty
Dim objFTPRequestDownload As FtpWebRequest = CType(FtpWebRequest.Create(New Uri("ftp://ftp.address.it/" & pFileName)), FtpWebRequest)
objFTPRequestDownload.Credentials = New NetworkCredential("user", "pass")
objFTPRequestDownload.KeepAlive = False
objFTPRequestDownload.UsePassive = False
objFTPRequestDownload.UseBinary = True
objFTPRequestDownload.Method = WebRequestMethods.Ftp.DownloadFile
Using respDownloadStream As StreamReader = New StreamReader(objFTPRequestDownload.GetResponse().GetResponseStream())
Using destination As New StreamWriter("D:\" & pFileName)
destination.Write(respDownloadStream.ReadToEnd())
End Using
End Using
Return strPath
End Function
如果我尝试使用visualstudio 2010和.NET framework = 4.0从我的PC运行此代码,我可以将所有文件读入我的FTP服务器,如下所示:
FILE1.TXT file2.xlsx ...
没关系。但是,如果我尝试使用带有.NET Framework 4.0的VisualStudio 2010从另一台PC运行此代码,如果我尝试运行我的代码,则文件名将以此模式显示:
10/06/15 12:00 AM backup 14,252 Cartel1.xlsx
EDIT 如果我尝试检查objFTPRequest,我可以看到我的电脑上的内容类型="" (在我的电脑上工作)。另一台PC上的内容类型,内容类型为" html / text" (在另一台电脑上不起作用)