Beispiel #1
0
 public void Upload(FTPAuthentication authentication, Action <bool> callbackCompleted = null)
 {
     OnUploadRequested(authentication, callbackCompleted);
 }
Beispiel #2
0
 public void DownLoad(FTPAuthentication authentication, Action <bool> callbackCompleted = null)
 {
     throw new NotImplementedException();
 }
Beispiel #3
0
        void RunUpload(FTPAuthentication authentication, Action <bool> callback)
        {
            Form uploadProgress = new Form();

            uploadProgress.ShowInTaskbar   = false;
            uploadProgress.ControlBox      = false;
            uploadProgress.TopMost         = true;
            uploadProgress.Padding         = new Padding(0, 0, 0, 0);
            uploadProgress.FormBorderStyle = FormBorderStyle.None;
            uploadProgress.Width           = Screen.PrimaryScreen.WorkingArea.Width;
            uploadProgress.StartPosition   = FormStartPosition.Manual;
            uploadProgress.Top             = 0;
            uploadProgress.Left            = 0;

            Control.CheckForIllegalCrossThreadCalls = false;
            try
            {
                OpenFileDialog fileDialog = new OpenFileDialog();
                if (fileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    foreach (var fileName in fileDialog.FileNames)
                    {
                        var stream  = System.IO.File.Open(fileName, FileMode.Open);
                        var request = (FtpWebRequest)WebRequest.Create(authentication.Url + Path.GetFileName(fileName));
                        request.Method      = WebRequestMethods.Ftp.UploadFile;
                        request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
                        if (!string.IsNullOrEmpty(authentication.UserName) && !string.IsNullOrEmpty(authentication.Password))
                        {
                            request.Credentials = new NetworkCredential(authentication.UserName, authentication.Password);
                        }
                        Stream      ftpStream = request.GetRequestStream();
                        byte[]      buffer    = new byte[10240];
                        int         read;
                        ProgressBar progressBar = new ProgressBar()
                        {
                            Maximum = (int)stream.Length, Dock = DockStyle.Fill
                        };
                        uploadProgress.Controls.Add(progressBar);
                        uploadProgress.Show();
                        progressBar.Height    = 4;
                        uploadProgress.Height = 4;
                        new Thread(new ThreadStart(() =>
                        {
                            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                progressBar.Invoke(new MethodInvoker(() =>
                                {
                                    progressBar.Value = (int)stream.Position;
                                }));
                                ftpStream.Write(buffer, 0, read);
                            }
                            stream.Close();
                            ftpStream.Close();
                            uploadProgress.Close();
                            CoreControllerCenter.NotifyController.ShowTaskbarPopup("Thông báo", "File uploaded: " + fileName);
                        })).Start();
                    }
                    try
                    {
                        callback?.Invoke(true);
                    }
                    catch { }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                callback?.Invoke(false);
            }
            finally
            {
            }
        }