Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
5.4k views
in .NET FTP by (240 points)
I must upload multiply image files in sizes average under 100kB.

I use Free version of edtFTP library.

1. Whats better using the FTPConnection or FTPClient ???

2. How to check thats a directory exist???

3. How to make visible that bytestransferred are updated. When i send the file the application stops and run after upload.

7 Answers

0 votes
by (51.6k points)
1. Whats better using the FTPConnection or FTPClient ???

FTPConnection

2. How to check thats a directory exist???

Try to change into it using FTPConnection.ChangeDirectory(string). If it throws an exception then the directory does not exist.

3. How to make visible that bytestransferred are updated. When i send the file the application stops and run after upload.

Firstly, if you really only want to be notified that the file upload has completed then you should use the FTPConnection.Uploaded event. Secondly, can you please post the code that you've put in your event-handler? We should be able to tell you how the deadlock occurs if we see the code.

- Hans (EnterpriseDT)
0 votes
by (240 points)
3. How to make visible that bytestransferred are updated. When i send the file the application stops and run after upload.

Firstly, if you really only want to be notified that the file upload has completed then you should use the FTPConnection.Uploaded event. Secondly, can you please post the code that you've put in your event-handler? We should be able to tell you how the deadlock occurs if we see the code.


I finally get a way to do it but i think it must be a better way than a thread. This is my solution:

namespace PolaczenieFTP
{
    public partial class Form1 : Form
    {
        
        public string[] filenames;
        public FTPConnection ftp;
        public OpenFileDialog openFile;
        public FileInfo fi;
        Thread watek; 

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFile = new OpenFileDialog();
            openFile.Filter = "All files (*.*)|*.*";
            openFile.Multiselect = true;
            openFile.RestoreDirectory = true;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                filenames = openFile.FileNames;

                ftp = new FTPConnection();
                ftp.ServerAddress = "xxxx";
                ftp.UserName = "xxxx";
                ftp.Password = "xxxx";

                ftp.ConnectMode = FTPConnectMode.PASV;
                ftp.DeleteOnFailure = true;
                ftp.TransferType = FTPTransferType.BINARY;

                ftp.EventsEnabled = true;
                ftp.TransferNotifyInterval = 4000;
                ftp.BytesTransferred += new BytesTransferredHandler(ftp_B);
                
                ftp.Connect();

                ftp.Uploaded += new FTPFileTransferEventHandler(zuploadowany);

                watek =  new Thread(new ThreadStart(upload));
                watek.Start();

            }
            
        }

        public void zuploadowany(object s, FTPFileTransferEventArgs ex) {
            listBox1.Items.Add(ex.RemoteFile.ToString()+" zdarzenie dziala");

        }

        public void ftp_B(object s, BytesTransferredEventArgs e)
        {
            label1.Text = e.ByteCount.ToString()+"/"+fi.Length;
        }

        public void upload()
        {
            for (int i = 0; i < filenames.Count(); i++)
            {
                fi = new FileInfo(filenames[i]);
                ftp.UploadFile(filenames[i], "nowy" + i + ".jpg");
            }
        }

        public void formClosing(object sender, FormClosingEventArgs ev)
        {
            try
            {
                watek.Abort();
            }
            catch (NullReferenceException e) { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            watek.Abort();
        }
    }
}

Can i give upload nad another methods in another class and then put it into a thread or thats will be bad solution ???
In uploaded event will be some code that puts information about images in MySQL database. (after uploading an image)
Please tell me is that good way to do it. It will be an service application to read some orders for an image(format,how many...) adding galeries and images to the galeries...
Thank you for the reply.
0 votes
by (51.6k points)
You're on the right track.

In Windows all of an applications GUI work happens on a single thread. So if, for example, a user presses two buttons in quick succession then the event-handler for the first button will be called immediately, but the one for the second button won't be called until the code in the first event-handler has finished running. The same goes for all other GUI events. Therefore, if an event-handler has code that takes a long time to run (such as uploading a large file) then no other event-handlers will be called until the upload has completed. In fact, since the repainting of the form itself takes place in an event-handler, the form won't even get redrawn while another event-handler is running. The application therefore appears to have frozen even though it actually hasn't.

edtFTPnet/Express addresses this problem by introducing a set of asynchronous methods, such as BeginUploadFile() and BeginDownloadFile(). These methods don't hold up the GUI thread but instead launch a worker-thread that executes slow operations in the background. Once the operation completes the user's callback method is invoked. This means developers don't have to mess around with multi-threaded code like you're having to.

So that's basically what you've discovered the hard way. Your code looks fine so good luck with it.

- Hans (EnterpriseDT)
0 votes
by (240 points)
I have read about asynchronous methods but i think it's to hard for me one thread is that what i need. How is it with resuming the files how to use this option?
Is it possible to write a code that in moment of closing form sends the unsended filenames into a file.txt and when next time running application resuming the files. I saw that when i don't abort the thread in moment of form_closing event then it uploaded anyway to the end all files.
I write this code for my eingeners work. It must be functionally but not a fully FTP client :)

I have one another question. How to upload with the free version files in different directories. It is neccesary to make

ftp.changeWorkingDirectory(destination); //set the destination of gallery
ftp.uploadFile("xxx.jpg","xxx.jpg"); //upload files into it
ftp.changeWorkingDirectoryUp(); //after multiple upload back to the ftp.serverDirectory (is this the root directory for me?? when i make another ftp.changeWorkingDirectoryUP() and ftp.serverDirectory = "/www.zdjecia-gogolin.pl/strona" it goes to the
path /www.zdjecia-gogolin.pl or it stays on ftp.serverDirectory ?
0 votes
by (51.6k points)
Is it possible to write a code that in moment of closing form sends the unsended filenames into a file.txt and when next time running application resuming the files.


Sure that's certainly an option. One way to do it would be to start with a list of the names of all the files that are to be uploaded; handle the Uploaded event and in the handler remove the name of the file that has just been uploaded from the list; and finally in the Form.Closing event write the list of the remaining files to a file.

I saw that when i don't abort the thread in moment of form_closing event then it uploaded anyway to the end all files.
I write this code for my eingeners work. It must be functionally but not a fully FTP client :)


Try calling FTPConnection.CancelTransfer in the Form.Closing event.

How to upload with the free version files in different directories.


You can either include the full path in when you call the UploadFile method, or change to the appropriate directory before calling the method and then pass in just the file-name.

- Hans (EnterpriseDT)
0 votes
by (160 points)
2. How to check thats a directory exist???

Try to change into it using FTPConnection.ChangeDirectory(string). If it throws an exception then the directory does not exist.


Well, if I try to create a directory that already exists, the program will crash.
How do I just let it continue running, however let it know that the directory does exist?
0 votes
by (162k points)
If you try to create a directory, just catch the exception which is thrown if it already exists. (you will also get an exception if you don't have permission to create the directory).

It is best though to try to change into the directory to ascertain if it exists (and catch the exception if it does not).

Categories

...