I have a website on ASP.NET Framework 4.8. It downloads images from the ftp server. During the test run of the project from a working computer, everything works as it should, i.e. everything is downloaded normally. But when I publish a project with the same data to the server, an ArgumentException error occurs. Below is the try block in which it occurs:
try
{
FileManager FM = new FileManager();
using (MemoryStream ms = new MemoryStream(
FM.ReadFile("***link to ftp***/Documents/" + "TechStoreDocuments/" + FileName,
Convert.ToInt64(DT.Rows[0]["FileSize"]), 1)))
{
Image img = Image.FromStream(ms);
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\TechStore", FileName);
img.Save(path, ImageFormat.Jpeg);
}
return FileName;
}
catch
{
return null;
}
If I catch an error in the ReadFile method, then "WebException: unable to connect to a remote server" pops up in the FtpWebResponse response = (FtpWebResponse)FTPClient line.GetResponse():
public byte[] ReadFile(string Path, Int64 FileSize, int FTPType)
{
CurrentSpeed = 0;
iTransData = 0;
Position = 0;
TotalFileSize = 0;
Byte[] buffer = new Byte[2048];
using (MemoryStream ms = new MemoryStream())
{
try
{
FtpWebRequest ftpClient = (FtpWebRequest)WebRequest.Create(Path);
if (FTPType == 3)
ftpClient.Credentials = new NetworkCredential(ServerFTP178Login, ServerFTP178Pass);
if (FTPType == 2)
ftpClient.Credentials = new NetworkCredential(LocalFTPLogin, LocalFTPPass);
if (FTPType == 1)
ftpClient.Credentials = new NetworkCredential(HostFTPLogin, HostFTPPass);
if (FTPType == 0)
ftpClient.Credentials = new NetworkCredential(ServerFTPLogin, ServerFTPPass);
ftpClient.UseBinary = true;
ftpClient.KeepAlive = true;
TotalFileSize = FileSize;
ftpClient.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)ftpClient.GetResponse();
//write file
Stream responseStream = response.GetResponseStream();
int bytesRead = responseStream.Read(buffer, 0, buffer.Length);
Position += bytesRead;
if (bytesRead > 0)
ms.Write(buffer, 0, bytesRead);
while (bytesRead > 0)
{
if (bStopTransfer)
{
bStopTransfer = false;
ftpClient.Abort();
response.Close();
responseStream.Close();
break;
}
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
ms.Write(buffer, 0, bytesRead);
Position += bytesRead;
}
Position = TotalFileSize;
ftpClient.Abort();
response.Close();
responseStream.Close();
ms.Capacity = (int)ms.Length;
return ms.ToArray();
}
catch (Exception ex)
{
bStopTransfer = false;
GC.Collect();
return ms.ToArray();
}
}
}