I have an application that uploads files to the ftp server (flash drive) of my router, there are not so many of them. And they are not large, up to 20 mb, but there are also quite small ones (txt). My method fires for every file, and looks like this:
private void UploadFileFtp(string filePathTemp, Uri filePathFtp, IProgress<float> progressOneFile)
{
FtpWebRequest ftpWeb = FtpWebRequest.Create(filePathFtp) as FtpWebRequest;
ftpWeb.Method = WebRequestMethods.Ftp.UploadFile;
ftpWeb.UseBinary = true;
using Stream streamRequest = ftpWeb.GetRequestStream();
using FileStream reader = new FileStream(filePathTemp, FileMode.Open);
var lenghtBytes = ftpWeb.ContentLength = reader.Length;
int @byte = reader.ReadByte();
long countBytes = 1;
int oldPrecent = 0;
while (@byte != -1)
{
streamRequest.WriteByte((byte)@byte);
@byte = reader.ReadByte();
int precent = (int)(countBytes++ * 100 / lenghtBytes);
if (precent != oldPrecent)
{
oldPrecent = precent;
progressOneFile.Report(precent / 100f);
}
}
streamRequest.Flush();
streamRequest.Close();
using FtpWebResponse response = ftpWeb.GetResponse() as FtpWebResponse;
if (!response.StatusDescription.Contains("226") && response.StatusCode != FtpStatusCode.ClosingData)
{
throw new Exception("Error in fileUnploading" + filePathTemp);
}
}
When testing on my computer (local network) everything works well. And on a laptop from an external Internet network, everything is fine. But from my friend's computer, it gives an error. ErorrScreen. And his router (DIR-825) freezes until you reboot it. And on my router, the stream with the file recording hangs for a while (Because it probably remains open). It always breaks on the same file, but if it is excluded, it breaks on another.
We tried disabling the firewall on his router. I also tried it on mine. It did not help. I have tried toggling different properties of KeepAlive , UsePassive , UseBinary , ConnectionGroupName. Which didn't help either. Then I ReEngenering the method:
private void UploadFileFtp(string filePathTemp, Uri filePathFtp, IProgress<float> progressOneFile)
{
FtpWebRequest ftpWeb = FtpWebRequest.Create(filePathFtp) as FtpWebRequest;
ftpWeb.Method = WebRequestMethods.Ftp.UploadFile;
ftpWeb.UseBinary = true;
ftpWeb.ConnectionGroupName = "uploadftp";
using FileStream reader = new FileStream(filePathTemp, FileMode.Open);
var lenghtBytes = ftpWeb.ContentLength = reader.Length;
byte[] buffer = new byte[1024];
using Stream streamRequest = ftpWeb.GetRequestStream();
int @byte = reader.Read(buffer, 0, buffer.Length);
long countIterationWrite = @byte;
int oldPrecent = 0;
while (@byte != 0)
{
streamRequest.Write(buffer, 0, buffer.Length);
@byte = reader.Read(buffer, 0, buffer.Length);
int precent = (int)((countIterationWrite+= @byte) * 100 / lenghtBytes);
if (precent != oldPrecent)
{
oldPrecent = precent;
progressOneFile.Report(precent/100f);
}
}
streamRequest.Flush();
streamRequest.Close();
using FtpWebResponse response = ftpWeb.GetResponse() as FtpWebResponse;
if (!response.StatusDescription.Contains("226") && response.StatusCode != FtpStatusCode.ClosingData)
{
throw new Exception("Ошибка при загрузке файла " + filePathTemp);
}
}
And it worked, but besides, when setting the buffer size to 1 byte, it crashes again. Can anyone explain what's going on here? And what could be the reason?
Read metadata in Microsoft. The WriteByte(int) method uses the Write(byte[], int, int) method with a new byte[1] array. We found a problem in the network card driver, when writing a stream in one byte. The network card just stopped responding and responding to anything. And the router somehow restored its interaction during the reboot, although the banal reconnection of the cord did not help. Which led to an erroneous opinion about a malfunction in the router, but as it turned out, the router continued to function in order. It helped to update the driver to a new one. And I still began to use a buffer of 4kB, because it's faster and more profitable.