我正在尝试将Excel文件传输到sftp网站并且我的代码正常执行,但我在网站上看不到该文件。
private static void SendFile(string FileName)
{
FileStream rdr = new FileStream(FileName + ".csv", FileMode.Open);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://sftp.somesite.com");
HttpWebResponse resp;
req.Method = "Post";
req.Credentials = new NetworkCredential("UN", "PW", "Domain");
req.ContentLength = rdr.Length;
req.AllowWriteStreamBuffering = true;
Stream reqStream = req.GetRequestStream();
byte[] inData = new byte[rdr.Length];
int bytesRead = rdr.Read(inData, 0, Convert.ToInt32(rdr.Length));
reqStream.Write(inData, 0, Convert.ToInt32(rdr.Length));
rdr.Close();
}
我不确定我在上面的代码中做错了什么。提前感谢您的帮助。
答案 0 :(得分:4)
为什么不使用FtpWebRequest?
using System.Net;
using System.IO;
public class Ftp
{
private static void ftpUpload(string filename, string destinationURI)
{
FileInfo fileInfo = new FileInfo(filename);
FtpWebRequest reqFTP = CreateFtpRequest(new Uri(destinationURI));
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// use binary
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInfo.Length;
// Buffer size set to 2kb
const int buffLength = 2048;
byte[] buff = new byte[buffLength];
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
FileStream fs = fileInfo.OpenRead();
// Read from the file stream 2kb at a time
int cLen = fs.Read(buff, 0, buffLength);
// Do a while till the stream ends
while (cLen != 0)
{
// FTP Upload Stream
strm.Write(buff, 0, cLen);
cLen = fs.Read(buff, 0, buffLength);
}
// Close
strm.Close();
fs.Close();
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
正如尤金在#3中所说的
当您说“SFTP”时,您的意思是FTP over SSL或SSH文件传输协议。他们需要不同的方法。如果您确实使用SFTP,就像在SSH文件传输中那样,我认为您最好使用像SharpSSH这样的第三方库(如果可以的话)。(http://sshnet.codeplex.com/)
SFTP上的Wiki - http://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol