ASP.NET C # uploads MemoryStream content via FTPwebRequest

This should be fairly straight forward and load work. BUT, when I open the downloaded file on an FTP server, it shows binary data, which are just some weird characters that look like [] [] [] [], and its correct file size. how to add attributes or headers that say this file is XML?

    public bool ProcessBatch(MemoryStream memStream)
    {
        bool result = true;
        FTPaddress = DistributionResources.ftpServer;
        CompleteFTPPath = DistributionResources.ftpPath;

        request = (FtpWebRequest)FtpWebRequest.Create(FTPaddress + CompleteFTPPath);
        request.Credentials = new NetworkCredential("username", "password");
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        try
        {

            byte[] buffer = new byte[memStream.Length];

            memStream.Read(buffer, 0, buffer.Length);
            memStream.Close();

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(buffer, 0, buffer.Length);
            }

            //Gets the FtpWebResponse of the uploading operation
            response = (FtpWebResponse)request.GetResponse();
            Console.WriteLine(response.StatusDescription); //Display response

        }
        catch(Exception ex)
        {
            result = false;
        }

        return result;
    }

Many thanks

+3
source share
1 answer

Do not try to use request.UseBinary = true

, request.UseBinary = false. , , , , , .

, FTP- Windows, ascii put . , , .

+3

Source: https://habr.com/ru/post/1703129/


All Articles