Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c# - Archive or image is corrupted after uploading to FTP and downloading back with FtpWebRequest

I have two methods:

  1. Uploads files to the FTP Server
  2. Downloads Files from the Server.

Everything works perfectly with text or xml files. But when I'm trying to upload and then download an archive or an image I get the "windows cannot open the folder. the compressed zip file is invalid" error for the archives and almost the same for the images. What may be the problem?

Here is the listing of my methods:

Upload:

private string Upload(string Login, string Password, string FilePath, string FileName, string uuid, string FTPDir)
{
    string CreateDirectory = CreateFTPDirectory(Login, Password, uuid, FTPDir);

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPDir + uuid + "/" + FileName);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UseBinary = true;

    StreamReader sourceStream = new StreamReader(FilePath + FileName);
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    using (Stream S = request.GetRequestStream())
    {
        S.Write(fileContents, 0, fileContents.Length);
    }
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    response.Close();

    return response.StatusDescription;
}

Download:

private string Download(string Login, string Password, string FileName, string uuid, string FTPDir, string Destination)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPDir + uuid + "/" + FileName);
    request.UseBinary = true;
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.Credentials = new NetworkCredential(Login, Password);
    byte[] buffer = new byte[1024];

    using (var response = (FtpWebResponse)request.GetResponse())
    {

        using (var stream = response.GetResponseStream())
        {
            using (var fs = new FileStream(Destination, FileMode.OpenOrCreate))
            {
                int readCount = stream.Read(buffer, 0, 1024);

                while (readCount > 0)
                {
                    fs.Write(buffer, 0, readCount);
                    readCount = stream.Read(buffer, 0, 1024);                            
                }
            }
            return response.StatusDescription;
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are uploading a binary file (a bitmap image) as if it were a text file in UTF-8 encoding:

byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

That naturally corrupts the file.

You have to transfer binary files exactly as they are, bit by bit.

Moreover your technique is quite inefficient for potentially large image files. You keep whole file in memory at least twice.

The code, that you need, is actually much simpler than yours:

using (Stream fileStream = File.OpenRead(FilePath + FileName)
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}

Your download code is ok, but again, it can be simplified to:

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(Destination))
{
    ftpStream.CopyTo(fileStream);
}

For a full code, see Upload and download a binary file to/from FTP server in C#/.NET.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...