Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi Guys ,

I am trying to uplaod the file into the FTP server , below is my code : i am getting error as"system.net.webexception: The remote server returned an error: (530) Not logged in."

if (fileupload.HasFile)
        {        //create the path to save the file to        

            string fileName = "/" + fileupload.FileName;

            //Is the file too big to upload?
            int fileSize = fileupload.PostedFile.ContentLength;
            string maxsize = ConfigurationManager.AppSettings["MaxFileSize"];
            bool sflag = Convert.ToBoolean(ConfigurationManager.AppSettings["RestrictFileSize"]);
            if (sflag)
            {
                if (fileSize > (Convert.ToInt32(maxsize) * 1048576))
                {
                    UploadStatusLabel.Text = "Filesize is too large. Maximum file size permitted is " + maxsize + "MB";
                }
                else
                {
                    ////save the file to our local path        
                    string foldername = string.Empty;
                    string ftpUser = string.Empty;
                    string ftpPassword = string.Empty;
                    foldername = ConfigurationManager.AppSettings["FilePath"];
                    ftpUser = ConfigurationManager.AppSettings["FTPUser"];
                    ftpPassword = ConfigurationManager.AppSettings["FTPPassword"];
                    string userdomain = ConfigurationManager.AppSettings["FTPDomain"];
                    string filename = fileupload.PostedFile.FileName;

                    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(foldername)+ fileName);
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    // This example assumes the FTP site uses anonymous logon.
                    request.Credentials = new NetworkCredential(ftpUser.Normalize(), ftpPassword.Normalize(), userdomain.Normalize());
                    
                    // Copy the contents of the file to the request stream.
                    StreamReader sourceStream = new StreamReader(filename);
                    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                    sourceStream.Close();
                    request.ContentLength = fileContents.Length;

                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(fileContents, 0, fileContents.Length);
                    requestStream.Close();

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                    response.Close();

                }
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900