【问题标题】:image corrupted when Uploading to ftp using ftpwebrequest使用 ftpwebrequest 上传到 ftp 时图像损坏
【发布时间】:2013-04-05 04:53:33
【问题描述】:

我使用此代码将图像上传到 ftp。但图像已损坏。 我尝试上传的图像是Base64 string.i 转换为流并将其传递给 UpLoadImage。

 public static void UpLoadImage(Stream image, string target)
   {
    FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://www.examp.com/images/" + target);
    req.UseBinary = true;
    req.Method = WebRequestMethods.Ftp.UploadFile;
    req.Credentials = new NetworkCredential("UserNm", "PassWd");
    StreamReader rdr = new StreamReader(image);
    byte[] fileData = Encoding.UTF8.GetBytes(rdr.ReadToEnd());
    rdr.Close();
    req.ContentLength = fileData.Length;
    Stream reqStream = req.GetRequestStream();
    reqStream.Write(fileData, 0, fileData.Length);
    reqStream.Close();
}

代替:

StreamReader rdr = new StreamReader(image); byte[] fileData = Encoding.UTF8.GetBytes(rdr.ReadToEnd()); rdr.Close();

如果我使用byte[] fileData = File.ReadAllBytes(image); 它给了我一个错误,文件名超过 260 个字符。

请有人帮忙解决这个问题..

【问题讨论】:

    标签: c# ftp


    【解决方案1】:

    就像评论说的 使用

    byte[] fileData = File.ReadAllBytes(filePath);
    

    对于所有不是文本文件的文件。

    像这样:

    private void FTPUPLOAD(string imgPath)
            {
                FTPDelete(img);
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(BaseUrl +Path.GetFileName(imgPath));
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential(Account, Password);
    
                // Copy the contents of the file to the request stream.
                 //THIS IS THE CODE
                byte[] fileContents = File.ReadAllBytes(imgPath);
    
     Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
    
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                response.Close();
            }
    

    【讨论】:

      【解决方案2】:

      您应该使用 Stream 来读取二进制文件,而不是 StreamReader。 StreamReader 仅用于读取文本文件。

      关于错误Maximum Path Length Limitation

      Windows API 中的最大路径长度限制(有些 以下段落中讨论的例外情况),最大长度 路径是 MAX_PATH,定义为 260 个字符。本地人 路径按以下顺序构建:驱动器号、冒号、 反斜杠,由反斜杠分隔的名称组件和终止 空字符。例如,驱动器 D 上的最大路径是“D:\some 256 个字符的路径字符串" 其中 "" 表示不可见 当前系统代码页的终止空字符。 (这 字符 在此处用于视觉清晰,不能作为其中的一部分 一个有效的路径字符串。)

      路径限制:http://msdn.microsoft.com/en-us/library/system.io.pathtoolongexception.aspx

      已编辑

      我写了一个简单的: 将图像转换为 b64 并返回

      //Convert image to b64
                  string path = @"E:\Documents and Settings\Ali\Desktop\original.png";
                  Image img = Image.FromFile(path);
                  byte[] arr;
                  using (MemoryStream ms = new MemoryStream())
                  {
                      img.Save(ms, ImageFormat.Jpeg);
                      arr = ms.ToArray();
                  }
                  String b64 = Convert.ToBase64String(arr);//result:/9j/4AAQSkZJRgABAQEA...
                  //Get image bytes
                  byte[] originalimage= Convert.FromBase64String(b64);
      

      将 b64 发送到您的函数并将其转换回 byte[] originalimage= Convert.FromBase64String(b64);

      【讨论】:

      • byte[] fileData = File.ReadAllBytes(image) 需要一个字符串作为参数。
      • @user2226345:File.ReadAllBytes("你的图片地址"),我认为你的路径是 >260 字符
      • 当我将 base64 字符串传递给 File.ReadAllBytes(image) 时,它显示文件名或路径过长(超过 260 个字符)的错误
      • 你的路径 >260 个字符
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      相关资源
      最近更新 更多