Stream 使用 C# 仅将 512 字节写入 FTP 服务器上的文件
Stream writes only 512 bytes to a file on FTP server using C#
我正在尝试使用 C# 将文件从本地计算机复制到 FTP 服务器。
当我使用下面的代码时,文件被完全复制到 FTP 服务器,但 ** 原始行被切割成只有 512 字节长的片段,而它们应该是 1152,1126 或 1024 字节长。 ** 我使用的示例文件现在有 16 行而不是 7 行。
public void uploadLOTFILE(string username, string password)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.mine/mypathandfilename");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = true;
System.IO.Stream rs = request.GetRequestStream();
var lines = File.ReadLines(@"myLocalFile.txt");
foreach (var line in lines)
{
byte[] buffer = Encoding.ASCII.GetBytes(line);
Console.WriteLine("buffer.length:" + buffer.Length.ToString());
rs.Write(buffer,0,buffer.Length);
}
rs.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
Console.writeline 示例文件的输出:
buffer.length:1152
buffer.length:1126
buffer.length:1152
buffer.length:1152
buffer.length:1152
buffer.length:1152
buffer.length:1024
我也使用了来自 msdn (https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx) 的精确副本,但结果相同。
编辑:
还尝试了以下代码:
字符串文件路径 = @"myFilePath";
var 文件名 = Path.GetFileName(文件路径);
var request = (FtpWebRequest)WebRequest.Create("ftp://myftp");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
using (var fileStream = File.OpenRead(filePath))
{
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
requestStream.Close();
}
}
它给出了相同的结果。文件已完全复制,但每 512 字节添加一个换行符。
使用 FileZilla,我可以正确地 FTP 传输相同类型的文件。
我使用可使用 cmd 访问的内置 windows FTP 解决了我的问题。
string[] lines = { username, password, "remote directory", "put " + "\"" + filePath + "\"" , "bye"};
// WriteAllLines creates a file, writes a collection of strings to the file,
// and then closes the file. You do NOT need to call Flush() or Close().
System.IO.File.WriteAllLines("full file path of your script", lines);
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine(@"ftp -s:" + "\"" + "full file path of your script" + "\"" + " remote device");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
我正在尝试使用 C# 将文件从本地计算机复制到 FTP 服务器。 当我使用下面的代码时,文件被完全复制到 FTP 服务器,但 ** 原始行被切割成只有 512 字节长的片段,而它们应该是 1152,1126 或 1024 字节长。 ** 我使用的示例文件现在有 16 行而不是 7 行。
public void uploadLOTFILE(string username, string password)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.mine/mypathandfilename");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = true;
System.IO.Stream rs = request.GetRequestStream();
var lines = File.ReadLines(@"myLocalFile.txt");
foreach (var line in lines)
{
byte[] buffer = Encoding.ASCII.GetBytes(line);
Console.WriteLine("buffer.length:" + buffer.Length.ToString());
rs.Write(buffer,0,buffer.Length);
}
rs.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
Console.writeline 示例文件的输出:
buffer.length:1152
buffer.length:1126
buffer.length:1152
buffer.length:1152
buffer.length:1152
buffer.length:1152
buffer.length:1024
我也使用了来自 msdn (https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx) 的精确副本,但结果相同。
编辑: 还尝试了以下代码: 字符串文件路径 = @"myFilePath"; var 文件名 = Path.GetFileName(文件路径); var request = (FtpWebRequest)WebRequest.Create("ftp://myftp");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
using (var fileStream = File.OpenRead(filePath))
{
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
requestStream.Close();
}
}
它给出了相同的结果。文件已完全复制,但每 512 字节添加一个换行符。
使用 FileZilla,我可以正确地 FTP 传输相同类型的文件。
我使用可使用 cmd 访问的内置 windows FTP 解决了我的问题。
string[] lines = { username, password, "remote directory", "put " + "\"" + filePath + "\"" , "bye"};
// WriteAllLines creates a file, writes a collection of strings to the file,
// and then closes the file. You do NOT need to call Flush() or Close().
System.IO.File.WriteAllLines("full file path of your script", lines);
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine(@"ftp -s:" + "\"" + "full file path of your script" + "\"" + " remote device");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();