我使用此代码将图像上传到 ftp。但图像已损坏。我试图上传的图像是一个Base64
字符串。我转换为流并将其传递给 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 个字符。
请有人帮忙解决这个问题..