This is my Code for uploading user input file to a ftp server:
public static bool UploadFile(string FilePath, HttpPostedFileBase file)
{
Stream ftpStream = null;
FtpWebRequest request = null;
FtpWebResponse response = null;
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL + FilePath);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Credentials = new NetworkCredential(UserName, Password);
ftpRequest.ContentLength = file.ContentLength;
Stream requestStream = ftpRequest.GetRequestStream();
Byte[] buffer = new Byte[buferSize];
int bytesRead = file.InputStream.Read(buffer, 0, buferSize);
while (bytesRead > 0)
{
requestStream.Write(buffer, 0, bytesRead);
bytesRead = file.InputStream.Read(buffer, 0, buferSize);
}
requestStream.Close();
return true;
}
catch (Exception)
{
return false;
}
}
and it seems work fine. but there is a problem. when user try to upload a file (a big one!) its takes a long time (to save file in host server memory or disk i think!) and take another! long time to upload in ftp. now the question is can i upload user input file through my host to ftp server directly. i mean instead using HttpPostedFile i have a stream from file to save it in Ftp server and reduce upload file time.
(Sorry for poor language and info!. im new in MVC.)