简体   繁体   English

上传到 ftp C 时文件名中的西里尔字母#

[英]Cyrillic in the file name when upload to ftp C #

I am writing a program that uploads a file to remote ftp server.我正在编写一个将文件上传到远程 ftp 服务器的程序。 However, downloaded files sometimes contain Cyrillic.但是,下载的文件有时包含西里尔字母。 The contents of the file load correctly, but the name is distorted.文件内容正确加载,但名称失真。 I understand that this is due to the encoding.我知道这是由于编码。 I tried to use Encoding.GetEncoding(1251).GetString(Encoding.GetEncoding(1251).GetBytes(file.FileName)) and other encodings.我尝试使用Encoding.GetEncoding(1251).GetString(Encoding.GetEncoding(1251).GetBytes(file.FileName))和其他编码。 Does not help.没有帮助。 At the same time, the php script that is stored on the same ftp correctly uploads files with cyrillic letters.同时,存储在同一个 ftp 上的 php 脚本可以正确上传带有西里尔字母的文件。

public void FtpUpload(IFormFile file, string filePath)
{
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xx.xxx/" + filePath);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential("username", "pass");
        byte[] fileBytes;
        if (file.Length > 0)
        {
            using (var ms = new MemoryStream())
            {
                file.CopyTo(ms);
                fileBytes = ms.ToArray();
            }
        }
        else return;

        request.ContentLength = fileBytes.Length;
        using (Stream request_stream = request.GetRequestStream())
        {
            request_stream.Write(fileBytes, 0, fileBytes.Length);
            request_stream.Close();
        }
}

filePath = "Тест.pdf"

I tried to use HttpUtility.UrlEncode(filePath) .我尝试使用HttpUtility.UrlEncode(filePath) Nothing helps.没有什么帮助。 Tell me, please, how to deal with this?请告诉我,如何处理?

Solution found.找到解决方案。 After scrolling through a few more forums, I came across the following solution:在浏览了更多论坛后,我遇到了以下解决方案:

Through NuGet Package Management install the package System.Net.FtpClient .通过NuGet Package Management安装 package System.Net.FtpClient Then we write the following code:然后我们编写如下代码:

public void FtpClientUpload(IFormFile file, string filePath)
    {
        FtpClient ftp = new FtpClient();
        ftp.Host = "xxx.xxx.xx.xxx";
        ftp.Credentials = new NetworkCredential("username", "pass");
        ftp.Encoding = Encoding.GetEncoding(1251);

        using (var remote = ftp.OpenWrite( filePath, FtpDataType.Binary))
            file.CopyTo(remote);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM