Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

FTP Uploading crash issue

$
0
0
Hey, so basically what the code does is, it reads the names and paths of the files opened via an openfiledialog, then it uploads those files one by one onto an ftp server while uploading an html file named after those individual files.
Everything works perfectly, when multiple files are selected,those files get uploaded along with an html file named after those respective files.
But if more than 2 files are selected from the openfiledialog the program crashes badly.
What am i doing wrong here because everything seems fine here to me.
  Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()

        If (dr = System.Windows.Forms.DialogResult.OK) Then
          
            ' Read the files 
            Dim N As Integer = 0
            For Each myFileName In OpenFileDialog1.FileNames
                Try

                    Dim MyAdvancedWebClient As New MyAdvancedWebClient
                    MyAdvancedWebClient.FileName = OpenFileDialog1.SafeFileNames(N)
                    Dim itm As New ListViewItem With {.Text = OpenFileDialog1.SafeFileNames(N)}
                    itm.SubItems.Add("0%")
                    ListView1.Items.Add(itm)
                    Dim myUri As New Uri("ftp://testing.com/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N))
                    MyAdvancedWebClient.Credentials = New System.Net.NetworkCredential("username", "password")


                    'Uploading of main file
                    MyAdvancedWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))


                    'Uploading the HTML file here

                    Dim b As New UriBuilder()
                    b.Host = "testing.com"
                    b.UserName = "username"
                    b.Password = "password"
                    b.Port = 21
                    b.Path = "/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N) & ".html"
                    b.Scheme = Uri.UriSchemeFtp
                    Dim g As Uri = b.Uri

                    Dim c As System.Net.FtpWebRequest = DirectCast(System.Net.FtpWebRequest.Create(g), System.Net.FtpWebRequest)
                    c.Method = System.Net.WebRequestMethods.Ftp.AppendFile
                    Dim h As System.IO.Stream = (c.GetRequestStream)
                    Dim SW As New System.IO.StreamWriter(h)
                    Dim contents = "<html><head></head><body></body></html>" & OpenFileDialog1.SafeFileNames(N)
                    SW.WriteLine(contents)

                    SW.Close()
                    h.Close()


                Catch SecEx As SecurityException
                    ' The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    ' Could not load the File - probably permissions-related.
                    MessageBox.Show(("Cannot Select File: " & myFileName.Substring(myFileName.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))

                End Try
                N = N + 1
            Next myFileName
    
        End If


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>