Error in File Uploading using ASP.Net

munirul islam 1 Reputation point
2021-04-08T04:57:00.873+00:00

Dear All
I am using following code snipped to upload file in SFTP. Its a console base application and working fine. File is uploaded in SFTP in a specific time.

public static void FileUploadSFTP(string sFileType)
{
// server credential configaration
string sHost = ConfigurationManager.AppSettings["hostname"].ToString();
string sUserName = ConfigurationManager.AppSettings["username"].ToString();
string sPassword = ConfigurationManager.AppSettings["password"].ToString();
int iPort = Convert.ToInt16(ConfigurationManager.AppSettings["port"].ToString());

        string ReadFileLoc = Convert.ToString(ConfigurationManager.AppSettings["ReadFileLoc"]);
        string uploadDirectory = Convert.ToString(ConfigurationManager.AppSettings["uploadDirectory"]);

        string sFileName = "";
        var host = sHost;
        var port = iPort;
        var username = sUserName;
        var password = sPassword;


        // path for file you want to upload
        // var uploadFile = @"c:yourfilegoeshere.txt";
        //select file name from the location D:\\DCC
        sFileName = ReadFileLoc + getFileName(sFileType);
        var uploadFile = sFileName;

        try
        {

            using (var client = new SftpClient(host, port, username, password))
            {
                client.Connect();
                // Change server directory
                client.ChangeDirectory(uploadDirectory);
                if (client.IsConnected)
                {
                    Debug.WriteLine("I'm connected to the client");
                    Console.WriteLine("Connected to SFTP server");
                    CLog.Logger.Write(CLog.INFORMATION, "/CFileUpload: I'm connected to the client ");
                    using (var fileStream = new FileStream(uploadFile, FileMode.Open))
                    {

                        client.BufferSize = 4 * 1024; // bypass Payload error large files
                        //Upload file
                        client.UploadFile(fileStream, Path.GetFileName(uploadFile));

                    }
                    CLog.Logger.Write(CLog.EXCEPTION, "/CFileUpload: Uploaded to server " + sFileName);
                }
                else
                {
                    Debug.WriteLine("I couldn't connect");
                    CLog.Logger.Write(CLog.EXCEPTION, "/CFileUpload: Cound not connect ");
                }
            }

        }
        catch (Exception exp)
        {
            CLog.Logger.Write(CLog.EXCEPTION, "/CFileUpload: Cound not connect " + exp.ToString());
        }


    }

I am getting following sometimes

This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server

Can you please help why how to resolve this issue.

Thanks in advance

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.