|
Hi every one,
strange issue, im trying to upload a whole simple one-page asp.net website to a remote ftp by my method called uploadFolderContents().
every structure and files gets uploade, but the contents of files are gone. all of them are empty even txt files.
i tried to upload a single txt file, the file appears on remote but empty as well.
oh, it also changes the source files, makes them all empty too!!!!
some hlp pls
clsFile.ftp ftp = new clsFile.ftp("ftp", "un", "pwd");
ftp.uploadFolderContents("httpdocs/omid", "E:\\omid");
public bool uploadFolderContents(string remoteFolder, string localFolder)
{
bool rslt = false;
try
{
string[] files = Directory.GetFiles(localFolder);
string[] dirs = Directory.GetDirectories(localFolder);
foreach (string file in files)
{
uploadFile(remoteFolder + "/" + Path.GetFileName(file), file);
}
foreach (string dir in dirs)
{
createFolder(remoteFolder + "/" + Path.GetFileName(dir));
uploadFolderContents(remoteFolder + "/" + Path.GetFileName(dir), dir);
}
rslt = true;
}
catch (Exception)
{
rslt = false;
}
return rslt;
}
public void uploadFile(string remoteFile, string localFile)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpStream = ftpRequest.GetRequestStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
modified 19-Feb-13 8:55am.
|
|
|
|
|
Omid Reza Aryafar wrote: FileStream localFileStream = new FileStream(localFile, FileMode.Create);
There's your problem:
Create: Specifies that the operating system should create a new file. If the file already exists, it will be overwritten.
You should be passing FileMode.Open to the FileStream constructor. Alternatively, use the File.OpenRead method[^] to open the file.
You should also wrap the localFileStream and ftpRequest in using blocks to make sure they're closed if anything goes wrong.
public void uploadFile(string remoteFile, string localFile)
{
try
{
using (var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile))
{
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
using (var ftpStream = ftpRequest.GetRequestStream())
using (var localFileStream = File.OpenRead(localFile))
{
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = localFileStream.Read(byteBuffer, 0, bufferSize);
while (bytesRead != 0)
{
ftpStream.Write(byteBuffer, 0, bytesRead);
bytesRead = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
wow, you did it.
tnx alot Richard.
im sorry to copy-paste uncarefuly and wasted your time.
tnx again.
|
|
|
|