FTP client in .netcore

孤街浪徒 提交于 2019-12-04 00:36:21

问题


Can I download file / list files via FTP protocol using netcoreapp1.0?

I know, I can use FtpWebRequest or FluentFTP if I target full .net45 framework.

My solution, however, is all based on .net standard 1.6 and I don't want to support full framework just to have FTP.


回答1:


FtpWebRequest is now included to .NET Standard 2.0

FluentFTP library is also compatible with latest .net standard 2.0




回答2:


FluentFTP now supports .NET core / .NET standard 1.6. If you run into problems please add an issue in the issue tracker and we'll work on it.




回答3:


There are no FTP capabilities out-of-the-box for netcoreapp1.0 or netstandard1.6. FtpWebRequest will return in netstandard2.0.




回答4:


FtpWebRequest is now supported in .NET Core 2.0. See GitHub repo

Example usage:

public static byte[] MakeRequest(
    string method, 
    string uri, 
    string username, 
    string password, 
    byte[] requestBody = null)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Credentials = new NetworkCredential(username, password);
    request.Method = method;
    //Other request settings (e.g. UsePassive, EnableSsl, Timeout set here)

    if (requestBody != null)
    {
        using (MemoryStream requestMemStream = new MemoryStream(requestBody))
        using (Stream requestStream = request.GetRequestStream())
        {
            requestMemStream.CopyTo(requestStream);
        }
    }

    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    using (MemoryStream responseBody = new MemoryStream())
    {
        response.GetResponseStream().CopyTo(responseBody);
        return responseBody.ToArray();
    }
}

Where the value for the method parameter is set as a member of System.Net.WebRequestMethods.Ftp.

See also FTP Examples




回答5:


You can try using FtpWebRequest method.

Sample:

public static byte[] DownloadFile(string url, string filePath, string user, string password)
        {
            var ftpServerUrl = string.Concat(url, filePath);
            var request = (FtpWebRequest) WebRequest.Create(ftpServerUrl);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential(user,password);
            using (var response = (FtpWebResponse) request.GetResponse())
            using (var responseStream = response.GetResponseStream())
            using (var memoryStream = new MemoryStream())
            {
                responseStream?.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }

Keep in mind, that the the ftpServerUrl has to be a valid uri path containing the file path. e.g. ftpServerUrl = "ftp://ftp.server/targetfile.txt"



来源:https://stackoverflow.com/questions/40600312/ftp-client-in-netcore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!