Example #1
0
        public byte[] Download(string Path, string Filename)
        {
            try
            {
                if (_ftpClient.DirectoryExists(Path))
                {
                    _ftpClient.RetryAttempts = 10;

                    string fullpath = Path + Filename;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        _log.Info($"{FtpTemplateMessage("Download", Filename, "Downloading")}", fullpath);
                        _ftpClient.Download(ms, fullpath, 0);

                        return(ms.ToArray());
                    }
                }
                else
                {
                    _log.Info($"{FtpTemplateMessage("Download", Filename, "Not Exist")}", Path + Filename);
                    throw new Exception("File Not Exist");
                }
            }
            catch (Exception ex)
            {
                _log.Error($"{FtpTemplateMessage("Download", Filename, "throw exception")}", ex);
                throw ex;
            }
        }
Example #2
0
        private void DownloadFile(IFtpClient client, ILocalDirectory storageFolder, string fullRemotePath, IRemoteItem item, bool overwrite)
        {
            var file = storageFolder.TryGetItemAsync(item.Name).Result;

            if (file != null && !overwrite)
            {
                var downloadRemoteItem = DependencyService.Resolve <IDownloadRemoteItem>();
                downloadRemoteItem.Destination = storageFolder;
                downloadRemoteItem.Item        = item;

                FileConflicts.Add(downloadRemoteItem);
                return;
            }

            storageFolder.CreateFileAsync(item.Name, overwrite)
            .ContinueWith(fileStreamTask =>
            {
                var stream = fileStreamTask.Result.OpenStreamForWriteAsync().Result;

                using (var fileStream = stream)
                {
                    client.Download(fileStream, fullRemotePath);
                }
            }).Wait();
        }
Example #3
0
 public static string DownloadToMemoryStream(this IFtpClient client, string name, MemoryStream memoryStream)
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     if (memoryStream == null)
     {
         throw new ArgumentNullException("memoryStream");
     }
     return(client.Download(name, (Stream stream) => {
         stream.CopyTo(memoryStream);
         memoryStream.Position = (long)0;
     }));
 }
Example #4
0
        public static string DownloadToString(this IFtpClient client, string name)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            string end = null;

            client.Download(name, (Stream stream) => {
                using (StreamReader streamReader = new StreamReader(stream))
                {
                    end = streamReader.ReadToEnd();
                }
            });
            return(end);
        }
Example #5
0
 public static string DownloadToLocal(this IFtpClient client, string name, DirectoryInfo localDirectory)
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     if (localDirectory == null)
     {
         throw new ArgumentNullException("localDirectory");
     }
     return(client.Download(name, (Stream stream) => {
         using (FileStream fileStream = new FileStream(Path.Combine(localDirectory.FullName, name), FileMode.Create))
         {
             stream.CopyTo(fileStream);
         }
     }));
 }
Example #6
0
        public static string DownloadToString(this IFtpClient client, string name)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            string content = null;

            client.Download(name, stream =>
            {
                using (var reader = new StreamReader(stream))
                {
                    content = reader.ReadToEnd();
                }
            });

            return(content);
        }