上传到FTP并使用FtpWebRequest下载后,归档或图像已损坏 [英] Archive or image is corrupted after uploading to FTP and downloading back with FtpWebRequest

查看:161
本文介绍了上传到FTP并使用FtpWebRequest下载后,归档或图像已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种方法:
$ b


  1. 将文件上传到FTP服务器

  2. 服务器。

一切都适用于文本或xml文件。但是,当我尝试上传并下载档案或图像时,我收到了窗口无法打开文件夹。压缩的zip文件无效的档案错误,并且图像几乎相同。可能是什么问题?

以下是我的方法清单:

上传:

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

FtpWebRequest请求=(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;

下载:

<$私人字符串下载(字符串登录,字符串密码,字符串文件名,字符串uuid,字符串FTPDir,字符串目的地)
{
FtpWebRequest请求=(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;




解决方案

您正在上传一个二进制文件(位图图像​​),就像它是UTF-8编码中的文本文件一样:

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

这自然会破坏文件。



您必须按照原样传输二进制文件,一点一点。



此外,对于潜在的大图像文件,您的技术效率非常低。您至少将整个文件保存在内存中至少两次。



您需要的代码实际上比您的代码简单得多:


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






<

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






完整代码请参阅使用C#/ .NET从FTP服务器上载和下载二进制文件


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;
        }
    }
}

解决方案

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.

这篇关于上传到FTP并使用FtpWebRequest下载后,归档或图像已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆