Upload multiple Files to FTP using FileUpload control in ASP.Net using C# and VB.Net
I am having a little issue with this – the first file selected uploads perfectly – every subsequent file is 0bytes. The code for the upload button is presented below.
Protected Sub cmdUpload_Click(sender As Object, e As EventArgs) Handles cmdUpload.Click Dim hfc As HttpFileCollection = Request.Files For i As Integer = 0 To hfc.Count - 1 If i < 9 Then team = "/Team0" & i + 1 & "/" If i > 8 Then team = "/Team" & i + 1 & "/" Dim myFtpWebRequest As FtpWebRequest Dim myFtpWebResponse As FtpWebResponse checkfolder() If direxists = False Then MakeDir(team) Dim hpf As HttpPostedFile = hfc(i) Dim filename As String = Path.GetFileName(hpf.FileName) myFtpWebRequest = CType(WebRequest.Create(ftppath + team + filename), FtpWebRequest) myFtpWebRequest.Credentials = New NetworkCredential(uname, pwd) myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile myFtpWebRequest.UseBinary = True Dim myFileStream As Stream = fileUpload.FileContent myFtpWebRequest.ContentLength = myFileStream.Length Dim requestStream As Stream = myFtpWebRequest.GetRequestStream() myFileStream.CopyTo(requestStream) requestStream.Close() requestStream.Dispose() myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse) myFtpWebResponse.Close() myFtpWebRequest = Nothing Next i lblSuccess.Text = "Upload completed." End Sub