从FTP服务器上载文件到目录 [英] Uploading a file to a Directory from an FTP server

查看:85
本文介绍了从FTP服务器上载文件到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我目前正在开发一个项目,在该项目中,客户端在手动执行操作时需要从ftp站点导入数据,并且需要使其自动化.
我想知道当用户单击按钮时我将如何允许用户执行该程序,该程序将从FTP服务器中提取所有文件并将它们添加到服务器上的目录(../Test)中. >
感谢

Hi
I am currently working on a project where my client needs to import data from an ftp site at the moment they are doing it manually and i need to automate it.
I would like to know how would I be able to allow the user when they click on the button , the program will fetch all the files from the FTP Server and add them into a directory(../Test) on my server

Thanks

推荐答案

导入System.Collections.Generic
导入实用程序.FTP

公共课程表格1

私有子Button1_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button1.Click
''如何使用FtpWebRequest的示例

''要使用的值
const localFile As String ="C:\ look.com"

const remoteFile As String ="/public_html/fm.com"
常量主机为String ="ftp://users.adelphia.net/"
常量用户名As String ="gcumbia"
const密码为String ="MyPassword"

''创建请求
昏暗的URI作为字符串=主机和remoteFile
昏暗的ftp作为System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI),System.Net.FtpWebRequest)
''设置凭据
ftp.Credentials =新的System.Net.NetworkCredential(用户名,密码)
''关闭KeepAlive(完成后将关闭连接)
ftp.KeepAlive = False
''我们想要一个二进制文件
ftp.UseBinary = True
''定义所需的操作(在这种情况下,请下载文件)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

``如果我们使用的是一种上传数据的方法,例如UploadFile
''我们将在这里打开ftp.GetRequestStream并发送数据

''获取对Ftp请求和相关流的响应
将响应用作System.Net.FtpWebResponse = CType(ftp.GetResponse,System.Net.FtpWebResponse)
使用responseStream作为IO.Stream = response.GetResponseStream
''循环读写文件
使用fs作为新的IO.FileStream(localFile,IO.FileMode.Create)
昏暗的缓冲区(2047)为字节
昏暗读取为整数= 0

读取= responseStream.Read(buffer,0,buffer.Length)
fs.Write(buffer,0,read)
循环直到读取= 0'',请参见Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
最终使用
responseStream.Close()
最终使用
response.Close()
最终使用

结束子

''FtpClient用法示例
私有子Button2_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button2.Click

''创建指向FtpServer的链接
昏暗的ftp作为新的FTPclient("users.adelphia.net","gcumbia","MyPassword")

''下载文件(允许覆盖)
''ftp.Download("/pub/ftpfile.bin","C:\ test \ ftp \ ftpfile.bin",True)

''上传文件
ftp.Upload("c:\ look.com","/public_html/look.com")

''文件存在吗?
"如果ftp.FtpFileExists("/pub/upload.exe")然后
''''重命名文件
" ftp.FtpRename("/pub/upload.exe","/pub/newname.exe")
''''删除文件
" ftp.FtpDelete("/pub/newname.exe")
''如果结束

''获得根目录的简单清单/
结果暗淡为List(Of String)= ftp.ListDirectory("/")
''在控制台中显示
Console.WriteLine("/的目录清单")
对于结果中的每一行作为字符串
Console.WriteLine(line)
下一个

''获取/pub/
的详细目录列表 昏暗的dirList为FTPdirectory = ftp.ListDirectoryDe​​tail("/public_html/")

''仅过滤列表中的文件
昏暗的文件仅作为FTPdirectory = dirList.GetFiles()

''下载这些文件
''对于每个文件作为FTPfileInfo在filesOnly中
''ftp.Download(文件,"C:\ test \ ftp \"&file.Filename,True)
''下一个


结束子
结束类
Imports System.Collections.Generic
Imports Utilities.FTP

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''EXAMPLE OF HOW TO USE FtpWebRequest

''Values to use
Const localFile As String = "C:\look.com"

Const remoteFile As String = "/public_html/fm.com"
Const host As String = "ftp://users.adelphia.net/"
Const username As String = "gcumbia"
Const password As String = "MyPassword"

''Create a request
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
''Set the credentials
ftp.Credentials = New System.Net.NetworkCredential(username, password)
''Turn off KeepAlive (will close connection on completion)
ftp.KeepAlive = False
''we want a binary
ftp.UseBinary = True
''Define the action required (in this case, download a file)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

''If we were using a method that uploads data e.g. UploadFile
''we would open the ftp.GetRequestStream here an send the data

''Get the response to the Ftp request and the associated stream
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
''loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 ''see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using

End Sub

''An example of FtpClient usage
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

''Create a link to an FtpServer
Dim ftp As New FTPclient("users.adelphia.net", "gcumbia", "MyPassword")

''download a file (permitting overwrite)
''ftp.Download("/pub/ftpfile.bin", "C:\test\ftp\ftpfile.bin", True)

''upload a file
ftp.Upload("c:\look.com", "/public_html/look.com")

''does a file exist?
''If ftp.FtpFileExists("/pub/upload.exe") Then
'' ''rename a file
'' ftp.FtpRename("/pub/upload.exe", "/pub/newname.exe")
'' ''delete a file
'' ftp.FtpDelete("/pub/newname.exe")
''End If

''get a simple listing of the root directory /
Dim result As List(Of String) = ftp.ListDirectory("/")
''display in console
Console.WriteLine("Directory listing of /")
For Each line As String In result
Console.WriteLine(line)
Next

''Get the detailed directory listing of /pub/
Dim dirList As FTPdirectory = ftp.ListDirectoryDetail("/public_html/")

''filter out only the files in the list
Dim filesOnly As FTPdirectory = dirList.GetFiles()

''download these files
''For Each file As FTPfileInfo In filesOnly
'' ftp.Download(file, "C:\test\ftp\" & file.Filename, True)
''Next


End Sub
End Class


这篇关于从FTP服务器上载文件到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆