我在 c# 中使用 FTP 服务器,我必须上传不同类型的文件(.png、.xlsx、docx ....),目前每种文件类型的上传都完成了,但是如果我尝试打开一个文件(.txt 除外)它表示该文件已损坏。我究竟做错了什么?
谢谢你
FtpWebResponse response = null;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
if (request == null)
{
result.SetError(Translate.InvalidUrl, url);
return false;
}
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
if (sourceStream == null)
{
result.SetError(Translate.FileErrorReading);
return false;
}
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();
response = (FtpWebResponse)request.GetResponse();
result.SetInformation(Translate.FileSuccefullUpload, filename);
}
catch (Exception e)
{
result.SetError(e.Message);
return false;
}
finally
{
if (response != null)
response.Close();
}
return result.Successful;
}