title: Zero size files uploaded with FTP FileUpload
body: <p>I've been reading gobs of articles on FTP upload in ASP.NET recently and they all seem to make sense, but every time I've tried implementing them I either get an empty file uploaded, or no file at all. Here are some of the articles I've been reading:</p>
  
  <ul>
  <li><a href="http://www.15seconds.com/Issue/080327.htm?voteresult=5" rel="nofollow noreferrer">Managing FTP Transfers from an ASP.NET Web Page</a> By John Peterson</li>
  <li><a href="http://weblogs.asp.net/ashicmahtab/archive/2009/05/20/fileupload-control-doesn-t-give-full-path-help.aspx" rel="nofollow noreferrer">FileUpload Control Doesn’t Give Full Path….HELP!!!!</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms227669%28v=VS.85%29.aspx" rel="nofollow noreferrer">How to: Upload Files with the FileUpload Web Server Control</a></li>
  </ul>
  
  <p>They're all great articles, but like I said, having issues :(</p>
  
  <p>I know exactly what the problem is but I don't know how to fix it. I can pass the file name from the FileUpload control, but the path does not exist for security concerns. However, the StreamReader object requires the fully qualified path of the file to be uploaded, so how the heck do I get that? I'm at my wits end! >.&lt;</p>
  
  <p>Let's use the example by John Peterson that I linked above. Here's the code:</p>
  
  <pre><code>Protected Sub btnUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      Dim myFtpWebRequest As FtpWebRequest
      Dim myFtpWebResponse As FtpWebResponse
      Dim myStreamWriter As StreamWriter
  
      myFtpWebRequest = WebRequest.Create("ftp://ftp_server_name/filename.ext")
      myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
      myFtpWebRequest.UseBinary = True
  
      myStreamWriter = New StreamWriter(myFtpWebRequest.GetRequestStream())
  
      'IT BREAKS HERE BECAUSE THE CLIENT PATH IS WRONG!!
      myStreamWriter.Write(New StreamReader(Server.MapPath("filename.ext")).ReadToEnd)
      myStreamWriter.Close()
  
      myFtpWebResponse = myFtpWebRequest.GetResponse()
      myFtpWebResponse.Close()
  End Sub
  </code></pre>
  
  <p>See? No data in the uploaded file :(</p>
  
  <p><img src="https://i.stack.imgur.com/vfJxB.png" alt="enter image description here"></p>
  
  <p>Now my latest implementation looks like this, but the uploaded file is much larger than the source, and corrupted. Seriously, what the heck am I doing wrong? I've been two LONG days at this, grrr...</p>
  
  <pre><code>Protected Sub btnUploadFile2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      Dim myFtpWebRequest As FtpWebRequest
      Dim myFtpWebResponse As FtpWebResponse
  
      filename = Path.GetFileName(FileUpload1.FileName)
  
      myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + filename), FtpWebRequest)
      myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
      myFtpWebRequest.UseBinary = True
  
      'NEW APPROACH USING THE STREAM OF THE FILE FROM THE FileUpload Control
      'CORRECT BYTE LENGTH - in sourceStream.BaseStream
      Dim sourceStream As New StreamReader(FileUpload1.FileContent)
      'WRONG BYTE LENGTH - in sourceStream.ReadToEnd()
      Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
      sourceStream.Close()
      myFtpWebRequest.ContentLength = fileContents.Length
  
      Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
      requestStream.Write(fileContents, 0, fileContents.Length)
      requestStream.Close()
  
      myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
      myFtpWebResponse.Close()
  End Sub
  </code></pre>
  
  <p>Thanks ever so much to Adam Maras for the amazing answer. I'll leave my blunders here for others to benefit who find this thread ;)</p>