我有一个简单的FTP上传(大多不是我自己的代码,仍在学习) 它工作得很好,但它正在破坏EXE文件,从我的阅读周围,这是因为它不是一个二进制阅读器。但令人困惑的是,我正在告诉它使用二进制。FTP FtpWebRequest uploader腐败EXE文件
这是我的代码:
private void UploadFileToFTP(string source)
{
String sourcefilepath = textBox5.Text;
String ftpurl = textBox3.Text; // e.g. ftp://serverip/foldername/foldername
String ftpusername = textBox1.Text; // e.g. username
String ftppassword = textBox2.Text; // e.g. password
try
{
string filename = Path.GetFileName(source);
string ftpfullpath = ftpurl + "/" + new FileInfo(filename).Name;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(source);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
怎么办你的意思是它工作正常,但腐败exe文件?它是如何正常工作,如果它是破坏文件? –
因为上传的实际功能适用于大多数文件类型。除了.exes –
然后听起来像是一个编码问题。也许OpenRead()对编码做一些简单的事情。试试我的例子。我只用EXE测试过,它工作正常。 – Cory