【问题标题】:File corrupted after upload to FTP server with WebRequest使用 WebRequest 上传到 FTP 服务器后文件损坏
【发布时间】:2015-11-20 17:39:09
【问题描述】:

这是将 .csv 文件传输到 FTP 服务器的一些问题。在传输之前还可以,但是当我在 FTP 上检查它时,它看起来像坏了之类的:

"ЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  T8пїЅпїЅпїЅпїЅпїЅпїЅ\p3>@L @E8?5=:>                                                                               BпїЅaпїЅ="

编码有问题吗?我正在使用这种下载方式:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxxxxxxx.xx/" + name);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.KeepAlive = true;
        request.UseBinary = true;
        request.Credentials = new NetworkCredential("xxxx", "qweqwe123");
        StreamReader sourceStream = new StreamReader("F:\\" + xxxx);

        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        response.Close();

【问题讨论】:

    标签: c# ftp webrequest ftpwebrequest


    【解决方案1】:

    不喜欢在我的问题下回答,但评论太长了: 解决了,可能有人遇到这个问题。尝试使用这种上传方式:

    FileInfo toUpload = new FileInfo("log.txt");
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://csharpcoderr.com/public_html/" + toUpload.Name);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential("name", "pass");
    Stream ftpStream = request.GetRequestStream();
    FileStream fileStream = File.OpenRead("log.txt");
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    do
    {
       bytesRead = fileStream.Read(buffer, 0, 1024);
       ftpStream.Write(buffer, 0, bytesRead);
    }
    while (bytesRead != 0);
    fileStream.Close();
    ftpStream.Close();
    

    【讨论】:

      【解决方案2】:

      我可以用同样的问题解决这个问题。 扩展 Brawl_Ups 解决方案... 似乎最好将 BinaryReader 用于二进制文件。 这是我偶然想到的一个sn-p... 这是一项正在进行的工作,欢迎进行任何编辑。

      public string UploadFile()
      {
          string sRetVal = string.Empty;
          string sFullDestination = this.DestinatinFullIDPath + this.UpLoadFileName;
          try
          {
              if ((this.CreateDestinationDir(this.DestinatinFullModulePath) == true) &
                  (this.CreateDestinationDir(this.DestinatinFullIDPath) == true))
              {
      
                  FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(sFullDestination);
                  ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                  ftpRequest.Credentials = new NetworkCredential(this.UserName, this.Password);
                  ftpRequest.UsePassive = true;
                  ftpRequest.UseBinary = true;
                  ftpRequest.EnableSsl = false;
      
                  byte[] fileContents;
                  using (BinaryReader binReader = new BinaryReader(File.OpenRead(this.UpLoadFullName)))
                  {
                      FileInfo fi = new FileInfo(this.UpLoadFullName);
                      binReader.BaseStream.Position = 0;
                      fileContents = binReader.ReadBytes((int)fi.Length);
                  }
      
                  ftpRequest.ContentLength = fileContents.Length;
                  using (Stream requestStream = ftpRequest.GetRequestStream())
                  {
                      requestStream.Write(fileContents, 0, fileContents.Length);
                  }
      
                  using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse())
                  {
                      sRetVal = string.Format("Upload File Complete, status {0}", response.StatusDescription);
                  }
              }
          }
          catch (WebException ex)
          {
              throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
          }
          return sRetVal;
      }
      

      【讨论】: