F#中的FTP编程

3

我需要用F#编写一个程序,将文件推送到FTP服务器。是否有可以使用的库?我在网上找不到任何信息。有人能指点我一下吗?如果可能的话,提供一些示例代码会非常有帮助。

3个回答

11

System.Net.WebRequest.Create 在给定ftp:// URL时表现良好。

要访问FTP特定的功能(例如上传文件),请将您的 WebRequest 对象强制转换为 FtpWebRequest


谢谢你的回复,Tim。当我尝试将 WebRequest 转换为 FtpWebRequest 时,我遇到了以下错误:“The value or constructor 'FtpWebRequest' is not defined”。这是我现在尝试进行转换的方式: let reqFTP: FtpWebRequest = (FtpWebRequest) (WebRequest.Create(uri))有什么想法吗? - sudaly
1
那是C#语法,不是F#。在F#中,您需要:let reqFTP: FtpWebRequest = WebRequest.Create(uri) :?> FtpWebRequest - Tim Robinson

4

0

内置的FtpWebRequest已经过时。FluentFTP是一个很棒的库,它经常得到更新。

#r "nuget: FluentFTP"

open FluentFTP

let ip = "45.33.2.79"
let username = "username"
let passwd = "s$cret"

let lpath = fsi.CommandLineArgs[1]
let rpath = fsi.CommandLineArgs[2]

let launch () =

    use con = new FtpClient(ip, username, passwd)
    con.Connect()

    let status =
        con.UploadFile(lpath, rpath, FtpRemoteExists.Overwrite, true, FtpVerify.Retry)

    match status with
    | FtpStatus.Success -> printfn "file uploaded OK"
    | FtpStatus.Failed -> printfn "failed to upload file"
    | _ -> printfn "skipped or unknown"


launch ()

该程序将本地文件上传到FTP服务器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接