Click here to Skip to main content
       

ASP.NET

 
GeneralRe: button disable until all required fields are filled PinmemberMember 870181321-Feb-13 5:00 
GeneralRe: button disable until all required fields are filled PinmemberMember 870181320-Feb-13 22:09 
QuestionPlease Wait .... Pinmemberjojoba201119-Feb-13 21:19 
AnswerRe: Please Wait .... PinmvpSandeep Mewara19-Feb-13 22:11 
AnswerRe: Please Wait .... PinmemberKarthik Harve20-Feb-13 19:07 
QuestionRe: Please Wait .... Pinmemberjojoba201122-Feb-13 23:26 
Questionftp uploaded files are empty [modified] PinmemberOmid Reza Aryafar19-Feb-13 2:43 
AnswerRe: ftp uploaded files are empty PinmemberRichard Deeming19-Feb-13 4:17 
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


GeneralRe: ftp uploaded files are empty PinmemberOmid Reza Aryafar19-Feb-13 4:34 
QuestionCan a Compare validator fire when either of two controls changes? Pinmemberdarookie9918-Feb-13 17:13 
AnswerRe: Can a Compare validator fire when either of two controls changes? PinmentorBrij18-Feb-13 19:09 
GeneralRe: Can a Compare validator fire when either of two controls changes? Pinmemberdarookie9919-Feb-13 13:51 
AnswerRe: Can a Compare validator fire when either of two controls changes? Pinmemberryanb3119-Feb-13 2:32 
AnswerRe: Can a Compare validator fire when either of two controls changes? PinmentorKeith Barrow19-Feb-13 4:07 
Questionmultilanguage application PinmemberMember 870181318-Feb-13 7:39 
AnswerRe: multilanguage application PinmvpSandeep Mewara18-Feb-13 7:58 
GeneralRe: multilanguage application PinmemberMember 870181325-Feb-13 21:59 
GeneralRe: multilanguage application PinmvpSandeep Mewara25-Feb-13 23:24 
GeneralRe: multilanguage application PinmemberMember 870181326-Feb-13 1:44 
GeneralRe: multilanguage application PinmvpSandeep Mewara26-Feb-13 2:04 
GeneralRe: multilanguage application PinmemberMember 870181326-Feb-13 2:51 
GeneralRe: multilanguage application PinmemberMember 870181326-Feb-13 5:20 
AnswerRe: multilanguage application PinmvpSandeep Mewara26-Feb-13 5:38 
GeneralRe: multilanguage application PinmemberMember 870181327-Feb-13 4:38 
GeneralRe: multilanguage application PinmvpSandeep Mewara27-Feb-13 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 20 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid