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());
myStreamWriter = myStream;
myReadStream = new StreamReader(Server.MapPath("~/Upload/") + FileUpload1.FileName);
myStreamWriter.Write(myReadStream.ReadToEnd());
myStreamWriter.Close();
myReadStream.Close();
myFtpWebResponse = (FtpWebResponse)myFtpWebRequest.GetResponse();
myFtpWebResponse.Close();
LblStatus.Text = "Status : File uploaded successfully";
}
catch (Exception ex)
{
LblStatus.Text = "Status : "+ ex.Message;
}