开发者

Monotouch - Error on download by FTP(oly zip files)

开发者 https://www.devze.com 2023-02-08 01:15 出处:网络
I\'m using this code for download files from a server via FTP. It works fine with almost all extensions(pdf, html, jpg...) but for some reason, all zip files are downloaded with some erros:

I'm using this code for download files from a server via FTP. It works fine with almost all extensions(pdf, html, jpg...) but for some reason, all zip files are downloaded with some erros:

public static FtpStatusCode Download(string destinationFile, Uri downloadUri, string userName, string password)
    {
        try
        {

            if (downloadUri.Scheme != Uri.UriSchemeFtp)
            {
                throw new ArgumentException("Invalid FTP site");
            }
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downl开发者_如何学PythonoadUri);
            ftpRequest.Credentials = new NetworkCredential(userName, password);
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            ftpRequest.UseBinary =true;
            ftpRequest.UsePassive = true;

            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream stream = null;
            StreamReader reader = null;
            StreamWriter writer = null;

            try
            {
                stream = ftpResponse.GetResponseStream();

                reader = new StreamReader(stream, Encoding.UTF8);
                writer = new StreamWriter(destinationFile, false);
                writer.Write(reader.ReadToEnd());

                return ftpResponse.StatusCode;
            }
            finally
            {

                stream.Close(); 
                reader.Close();
                writer.Close();     
            }

        }

        catch (Exception ex)
        {
            throw ex;
        }
    }

Does anybody know the reason or can tell a solution?

Regards,

Claudio


You are using a StreamReader to transfer your information, which is decoding binary data that is not valid UTF8 code, transforming it into lines of UCS2 and then re-encoding the result.

You should perform a copy without the StreamReader and the StreamWriter.

0

精彩评论

暂无评论...
验证码 换一张
取 消