Click here to Skip to main content
14,608,023 members

Ftp Upload - drivehq.com

Md Sadham asked:

Open original thread
Design:
After browse the file using fileUpload, there is one button onClick event followed. In that event, am upload the file which is in fileUpload to the driveHq.com ftp server.


Problem:
* Object reference not set to an instance of an object

Why its happening?

Note:
* User credentials are checked clearly
* Also checked, am still free member for this account and having free space for storing data.


CODE:

try
          {
                FileUpload1.SaveAs(Server.MapPath("~/Upload/") + FileUpload1.FileName);

                FtpWebRequest myFtpWebRequest;
                FtpWebResponse myFtpWebResponse;
                StreamWriter myStreamWriter;
                NetworkCredential myNetworkCredential;
                StreamWriter myStream;
                StreamReader myReadStream;                        
                
                myFtpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ftp.drivehq.com/" + FileUpload1.FileName));
                myNetworkCredential = new NetworkCredential();
                myNetworkCredential.UserName = txtfldUsername.Text;
                myNetworkCredential.Password = txtfldPassword.Text;
                myFtpWebRequest.Credentials = myNetworkCredential;
                myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
                myFtpWebRequest.UseBinary = true;

                myStream = new StreamWriter(myFtpWebRequest.GetRequestStream());// This line causes problem
                myStreamWriter = myStream;
                myReadStream = new StreamReader(Server.MapPath("~/Upload/") + FileUpload1.FileName);
                myStreamWriter.Write(myReadStream.ReadToEnd());
                myStreamWriter.Close();
                myReadStream.Close();

                myFtpWebResponse = (FtpWebResponse)myFtpWebRequest.GetResponse();
                //Label6.Text = myFtpWebResponse.StatusDescription;
                myFtpWebResponse.Close();
                LblStatus.Text = "Status : File uploaded successfully";               
            }
            catch (Exception ex)
            {
                LblStatus.Text = "Status : "+ ex.Message;
            }
Tags: C# (C#4.0), ASP.NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100