示例#1
0
        private async Task UploadFileAsync(FluentFTP.FtpClient client, string remotePath, StorageFile localFile, CancellationToken token, IProgress <double> progress)
        {
            await semaphore.WaitAsync();

            try
            {
                string remoteDirectory = Path.GetDirectoryName(remotePath);
                string remoteFileName  = Path.GetFileName(remotePath);
                while (!(await client.DirectoryExistsAsync(remoteDirectory)))
                {
                    string dir = Path.GetFileName(remoteDirectory);
                    remoteDirectory = Path.GetDirectoryName(remoteDirectory);
                    remoteFileName  = Path.Combine(dir, remoteFileName);
                }
                await client.SetWorkingDirectoryAsync(remoteDirectory);

                using (var stream = await localFile.OpenStreamForReadAsync())
                {
                    await client.UploadAsync(stream, remoteFileName, FluentFTP.FtpExists.Overwrite, true, token, progress);
                }
            }
            finally
            {
                client.Dispose();
                semaphore.Release();
            }
        }
示例#2
0
        public void Dispose()
        {
            if (_FtpClient == null)
            {
                return;
            }

            _FtpClient.Dispose();
        }
示例#3
0
        private async Task DownloadFileAsync(FluentFTP.FtpClient client, string remotePath, StorageFile localFile, CancellationToken token, IProgress <double> progress)
        {
            await semaphore.WaitAsync();

            try
            {
                string remoteDirectory = Path.GetDirectoryName(remotePath);
                string remoteFileName  = Path.GetFileName(remotePath);
                await client.SetWorkingDirectoryAsync(remoteDirectory);

                using (var stream = await localFile.OpenStreamForWriteAsync())
                {
                    await client.DownloadAsync(stream, remoteFileName, token, progress);
                }
            }
            finally
            {
                client.Dispose();
                semaphore.Release();
            }
        }
示例#4
0
        /// <summary>
        /// 导航到指定的地址。需要登录时自动弹出登录界面。导航失败时,自动导航到失败页面。
        /// 不抛出异常。
        /// </summary>
        /// <param name="address">要导航到的地址</param>
        /// <returns>导航是否成功</returns>
        private async Task <bool> NavigateAsync(Uri address)
        {
            await ftpSemaphore.WaitAsync();

            try
            {
                errorMessage.Visibility     = Visibility.Collapsed;
                progressBar.Visibility      = Visibility.Visible;
                progressBar.IsIndeterminate = true;

                currentAddress = address;
                // 显示新的地址
                UriBuilder uriBuilder = new UriBuilder(currentAddress);
                uriBuilder.UserName = string.Empty;
                uriBuilder.Password = string.Empty;
                addressBox.Text     = uriBuilder.Uri.ToString();


                // 不是ftp协议则抛出异常
                if (address.Scheme != "ftp" && address.Scheme != "ftps")
                {
                    throw new InvalidOperationException("只支持ftp协议");
                }

                // Host改变时重新连接
                if (client == null)
                {
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }
                else if (client.Host != address.Host ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.None && address.Scheme == "ftp") ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.Explicit && address.Scheme == "ftps") ||
                         (client.Port != 21 && address.Port < 0) ||
                         (client.Port != address.Port && address.Port >= 0) ||
                         !string.IsNullOrEmpty(address.UserInfo))
                {
                    client.Disconnect();
                    client.Dispose();
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }

                // FTP路径可能包含#号,#号后面的内容会被归入Fragment。
                string remotePath = address.LocalPath + address.Fragment;

                listItemsVM.Clear();
                foreach (var item in (await client.GetListingAsync(remotePath)).OrderBy(x => x.Name))
                {
                    listItemsVM.Add(await FtpListItemViewModel.FromFtpListItemAsync(item));
                }

                using (Data.HistoryContext db = new Data.HistoryContext())
                {
                    Data.HistoryEntry h = new Data.HistoryEntry
                    {
                        Time = DateTimeOffset.Now,
                        Url  = uriBuilder.Uri.ToString()
                    };
                    await db.AddAsync(h);

                    await db.SaveChangesAsync();
                }

                return(true);
            }
            catch (FluentFTP.FtpCommandException exception)
            {
                if (exception.CompletionCode == "530")
                {
                    errorMessage.Text = "认证失败,请尝试登录。";
                    loginButton.Flyout.ShowAt(loginButton);
                    loginErrorMessage.Visibility = Visibility.Visible;
                }
                else
                {
                    errorMessage.Text = string.Format(
                        "发生错误,FTP返回代码:{0}。详细信息:\n{1}", exception.CompletionCode, exception.Message);
                }

                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            catch (Exception exception)
            {
                errorMessage.Text       = string.Format("发生错误。详细信息:\n{0}", exception.Message);
                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            finally
            {
                progressBar.Visibility = Visibility.Collapsed;
                ftpSemaphore.Release();
            }
        }
示例#5
0
 public void Dispose()
 {
     _client.Dispose();
 }
示例#6
0
 public void Dispose()
 {
     ftp?.Dispose();
     ftp = null;
 }