Autor Beitrag
Talemantros
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 444
Erhaltene Danke: 2

Win7 Proff 64bit
C# (VS2013)
BeitragVerfasst: Mi 19.03.14 20:46 
Hallo,
ich möchte gern einen Download von einem FTP machen und habe dazu folgenden Code im Internet gefunden, der auch funktioniert.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
    class ftp
    {
        private string host = null;
        private string user = null;
        private string pass = null;
        private FtpWebRequest ftpRequest = null;
        private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;
        private int bufferSize = 2048;

        /* Construct Object */
        public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }

        /* Download File */
        public void download(string remoteFile, string localFile)
        {
            try
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                /* Establish Return Communication with the FTP Server */
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                /* Get the FTP Server's Response Stream */
                ftpStream = ftpResponse.GetResponseStream();
                /* Open a File Stream to Write the Downloaded File */
                FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                /* Buffer for the Downloaded Data */
                byte[] byteBuffer = new byte[bufferSize];
                int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
                try
                {
                    while (bytesRead > 0)
                    {
                        localFileStream.Write(byteBuffer, 0, bytesRead);
                        bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                /* Resource Cleanup */
                localFileStream.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            return;
        }


Jetzt würde ich das gern in einer Form mit Progressbar anzeigen bekomme es aber ums verrecken nicht hin.
Habe es schon mit Backgroundworkern und ähnlichen probiert und komme einfach zu keinem funktionalen Ergebnis.

Kann mir da bitte wer helfen?

VG
Daniel
IhopeonlyReader
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 600
Erhaltene Danke: 23


Delphi 7 PE
BeitragVerfasst: Mi 19.03.14 22:38 
ich kann zawr kein c#, aber du musst so denken:
- wenn ich den Fortschritt anzeigen will, muss ich wissen wieviel von wieviel (maximal) schon runtergeladen worden ist..

ich denke mal die graphische darstelleung ist weniger das problem!

so, dass heißt du musst erstmal irgendwoher wissen, wie groß die Datei insgesamt ist.
Ebenso musst du wissen wieviel du schon runtergeladen hast
(du lädst glaub ich in 2kiB Blöcken oder? dann könntest du pro "lesen" eines Blockes 2kiB auf den "schon runtergeladen" block dazuaddieren

Allgemein
- Gesamte des zu runterladenen Paketes ermittlen
- Datei runterladen ( teil für teil)
- nach dem download eines teils: runtergeladen := runtergeladen + teil.groesse
- anzeige (in%): (runtergeladen/Gesamtpaket.Groesse)*100

_________________
Sucht "neueres" Delphi :D
Wer nicht brauch was er hat, brauch auch nicht was er nicht hat!


Zuletzt bearbeitet von IhopeonlyReader am Di 25.03.14 21:55, insgesamt 1-mal bearbeitet
Talemantros Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 444
Erhaltene Danke: 2

Win7 Proff 64bit
C# (VS2013)
BeitragVerfasst: Di 25.03.14 21:02 
Vielen Dank für den Gedankenanstoß!
Hatte vergessen zu schreiben.
Sorry

Habe einen entsprechenden aus dem Netz für meine Zwecke umbauen können.
IhopeonlyReader
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 600
Erhaltene Danke: 23


Delphi 7 PE
BeitragVerfasst: Di 25.03.14 21:54 
freut mich, dass du dich wenigstens noch meldest :)
Wäre nett, wenn du vielleicht den Link hier auch noch reinstellst, damit andere, die diesen Thread finden, auch teilhaben können, an deinem Erfolg

Lg IHopeOnlyReader

_________________
Sucht "neueres" Delphi :D
Wer nicht brauch was er hat, brauch auch nicht was er nicht hat!
Talemantros Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 444
Erhaltene Danke: 2

Win7 Proff 64bit
C# (VS2013)
BeitragVerfasst: Mi 26.03.14 14:45 
Hi,
ja tut mir leid, dass es ein wenig gedauert hat.
Habe das verdrängt weil ich mich momentan über jeden kleinen Fortschritt in C# freue wie ein kleines Kind zu Weihnachten :-)

Mag es aber nicht unkommentiert lassen, da ich es beachtlich finde, wenn jemand seine Zeit und Wissen hier einsetzt. Das muss dann auch entsprechende Anerkennung bei rum kommen.

Ich habe folgendes Tutorial am Ende genommen:
Application Auto-update via Online Files in C#

Habe versucht, den ganzen Quellcode zu verstehen und habe die grafische Oberfläche etc. entfernt.
In Klassen für künftige Projekte ausgelagert.

War wieder ein kleines Stück nach vorne :-)

VG
Daniel

P.S.: Diesen Link fand ich als Anfänger am Verständlichsten

Moderiert von user profile iconTh69: URL-Tag hinzugefügt