Exemple #1
0
        public void DownloadFile(string url, string folder, string courseid)
        {
            CourseID = courseid;
            string pathToNewFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), folder);

            Directory.CreateDirectory(pathToNewFolder);

            Directory.CreateDirectory(pathToNewFolder);

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
                webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(Completed);
                string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName("CoursePackage.zip"));
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(ex.Message, false));
                }
            }
        }
Exemple #2
0
        public void DownloadFile(string url, string folder)
        {
            string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);

            Directory.CreateDirectory(pathToNewFolder);
            int count = 1;

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(Completed);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChange);
                pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
                while (File.Exists(pathToNewFile))
                {
                    string tempFileName = string.Format("{0}({1})", Path.GetFileNameWithoutExtension(url), count++);
                    pathToNewFile = Path.Combine(pathToNewFolder, tempFileName + Path.GetExtension(url));
                }
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception ex)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, pathToNewFile));
                }
            }
        }
Exemple #3
0
        public void DownloadFile(string url, string folder, string FileName)
        {
            var pathToNewFolder = Path.Combine(MainActivity.mainActivity.GetExternalFilesDir(null).AbsolutePath, folder);

            if (!Directory.Exists(pathToNewFolder))
            {
                Directory.CreateDirectory(pathToNewFolder);
            }
            string filePath = Path.Combine(pathToNewFolder, FileName);

            Directory.CreateDirectory(pathToNewFolder);
            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                string pathToNewFile = Path.Combine(pathToNewFolder, FileName);
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception ex)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                }
            }
        }
        public void DownloadFile()
        {
            string url    = _urls.Dequeue();
            string folder = _folders.Dequeue();

            string pathToNewFolder = Path.Combine(path1: System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), path2: "..", path3: "Library", path4: folder);

            if (Directory.Exists(pathToNewFolder))                           //bible 폴더가 존재하는 경우
            {
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(false)); // 이미 존재하므로 file download 불가능
                return;
            }
            Directory.CreateDirectory(pathToNewFolder);

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception ex)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                }
            }
        }
        public void DownloadFile(string url, string folder)
        {
            pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);
            //var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), dbName);
            pathToNewFolder = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), folder);
            //Constans.myZipPath = pathToNewFolder;
            Directory.CreateDirectory(pathToNewFolder);

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
                zipFileMentett = Path.GetFileName(url);
            }
            catch (Exception ex)
            {
                Constans.exception = ex;
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, zipFileMentett));
                }
            }
        }
Exemple #6
0
        public void DownloadFile(string url, string folder)
        {
            var IsPermissionGranted = CheckPermission();

            if (IsPermissionGranted == false)
            {
                RequestPermission();
                return;
            }

            try
            {
                string fileName = "smeta_" + DateTime.Now.ToString("dd_MM_yy") + ".pdf";

                Request request = new Request(Android.Net.Uri.Parse(url));

                // Store to common external storage:
                request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, fileName);
                request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
                request.SetDescription(fileName);
                request.SetTitle(fileName);

                DownloadManager dm      = (DownloadManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.DownloadService);
                var             enqueue = dm.Enqueue(request);
            }
            catch (Exception)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                }
            }
        }
Exemple #7
0
        public void DownloadFile(string url)
        {
            pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "Pictures", "Screenshots");
            try
            {
                urlGet = url;
                using (WebClient webClient = new WebClient())
                {
                    webClient.OpenRead(url);
                    double totalBytes = Convert.ToDouble(webClient.ResponseHeaders["Content-Length"]);

                    webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(Completed);
                    webClient.DownloadProgressChanged += (o, e) =>
                    {
                        double bytesIn    = e.BytesReceived;
                        double percentage = ((bytesIn / totalBytes) * 100);
                        MessagingCenter.Send <App, string>((App)Xamarin.Forms.Application.Current, "downloadUpdateProgress", Math.Truncate(percentage).ToString());
                    };

                    fileName = Path.GetFileName(url);
                    string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
                    webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
                }
            }
            catch (Exception ex)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                }
            }
        }
        public void DownloadFile(string url, string folder)
        {
            try
            {
                string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);
                Directory.CreateDirectory(pathToNewFolder);

                try
                {
                    WebClient webClient = new WebClient();
                    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                    string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
                    webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
                    count++;
                }
                catch (Exception ex)
                {
                    if (OnFileDownloaded != null)
                    {
                        OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                    }
                    string error = ex.Message;
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
        }
        public void DownloadFile(string url, string folder, string fileName)
        {
            try
            {
                string newFolderPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);
                Directory.CreateDirectory(newFolderPath);

                using (WebClient client = new WebClient())
                {
                    client.DownloadFileCompleted += FileDownloadCompleted;
                    filePath = Path.Combine(newFolderPath, fileName);
                    if (!File.Exists(filePath))
                    {
                        client.DownloadFileAsync(new Uri(url), filePath);
                    }
                    else
                    {
                        OnFileDownloaded?.Invoke(this, new DownloadEventArgs(true, filePath));
                    }
                }
            }
            catch (Exception)
            {
                OnFileDownloaded?.Invoke(this, new DownloadEventArgs(false, null));
            }
        }
Exemple #10
0
        public void DownloadFile(string url, string data, string name)
        {
            string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "QualityLift Images");

            Directory.CreateDirectory(pathToNewFolder);

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                string pathToNewFile = Path.Combine(pathToNewFolder, name);

                if (data != "No data")
                {
                    using (FileStream file = new FileStream(pathToNewFile, FileMode.Create, FileAccess.Write))
                    {
                        byte[] d = Convert.FromBase64String(data);
                        file.Write(d, 0, d.Length);
                        OnFileDownloaded?.Invoke(this, new DownloadEventArgs(true));
                    }
                }
                else
                {
                    webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
                }
            }
            catch (Exception)
            {
                OnFileDownloaded?.Invoke(this, new DownloadEventArgs(false));
            }
        }
 void Downloader_OnFileDownloaded(object sender, DownloadEventArgs e)
 {
     if (OnFileDownloaded != null)
     {
         OnFileDownloaded.Invoke(this, e);
     }
 }
        private void BtnDownloadSound_Click(object sender, RoutedEventArgs e)
        {
            string filePath   = @"C:\tmp\" + _sample.Name;
            var    fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);

            _sample.Download().CopyTo(fileStream);
            fileStream.Flush();
            fileStream.Dispose();

            OnFileDownloaded?.Invoke(filePath);
        }
        private void Unzip()
        {
            string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "TCLMS/Temp/CoursePackage.zip");
            string newFolder       = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "TCLMS/Courses/" + CourseID);
            string pathToNewFile   = Path.Combine(pathToNewFolder, Path.GetFileName("CoursePackage.zip"));

            ZipFile    zf = null;
            FileStream fs = File.OpenRead(pathToNewFolder);

            zf = new ZipFile(fs);

            foreach (ZipEntry zipEntry in zf)
            {
                String entryFileName = zipEntry.Name;
                // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
                // Optionally match entrynames against a selection list here to skip as desired.
                // The unpacked length is available in the zipEntry.Size property.

                byte[] buffer    = new byte[4096];  // 4K is optimum
                Stream zipStream = zf.GetInputStream(zipEntry);

                // Manipulate the output filename here as desired.
                String fullZipToPath = Path.Combine(newFolder, entryFileName);
                string directoryName = Path.GetDirectoryName(fullZipToPath);
                if (directoryName.Length > 0)
                {
                    Directory.CreateDirectory(directoryName);
                }

                // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                // of the file, but does not waste memory.
                // The "using" will close the stream even if an exception occurs.
                try
                {
                    using (FileStream streamWriter = File.Create(fullZipToPath))
                    {
                        StreamUtils.Copy(zipStream, streamWriter, buffer);
                    }
                }
                catch (Exception ex) {
                    Crashes.TrackError(ex);
                }
            }
            File.Delete(pathToNewFolder);


            if (OnFileDownloaded != null)
            {
                OnFileDownloaded.Invoke(this, new DownloadEventArgs("Course Downloaded", true, CourseID));
            }
        }
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(e.Error.Message, false));
         }
     }
     else
     {
         Unzip();
     }
 }
Exemple #15
0
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, ""));
         }
     }
     else
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(true, pathToNewFolder));
         }
     }
 }
Exemple #16
0
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, e.Error.Message));
         }
     }
     else
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(true, "File downloaded successfull"));
         }
     }
 }
Exemple #17
0
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, zipFileMentett));
         }
     }
     else
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(true, zipFileMentett));
         }
     }
 }
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         App.Current.MainPage.DisplayAlert("Error", e.Error.Message, "Ok");
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
         }
     }
     else
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
         }
     }
 }
Exemple #19
0
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
         }
     }
     else
     {
         System.Diagnostics.Debug.WriteLine(e.Error);
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
         }
     }
 }
Exemple #20
0
 public void DownloadFile(string url, string folder)
 {
     pathToNewFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), folder);
     try
     {
         WebClient webClient = new WebClient();
         webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
         string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
         webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
     }
     catch (Exception)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, ""));
         }
     }
 }
Exemple #21
0
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, zipFileMentett));
                }
            }
            else
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(true, zipFileMentett));
                }

                //Constans.ExtractZipFile(pathToNewFolder + "/" + zipFileMentett, null, pathToNewFolder);
            }
        }
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            var webClient = sender as WebClient;
            var fileName  = webClient.Headers.Get("PrivateFileName");

            if (e.Error != null)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, fileName));
                }
            }
            else
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(true, fileName));
                }
            }
        }
Exemple #23
0
        public void DownloadFile(string url, string folder)
        {
            string pathToNewFolder = Path.Combine(StorageDirectory, folder);

            Directory.CreateDirectory(pathToNewFolder);
            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
                string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception exp)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                }
            }
        }
Exemple #24
0
 private void WC_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
 {
     try
     {
         int fileIndex = (int)sender;
         File.WriteAllBytes(updateDir + updInfo.FilePath[fileIndex], e.Result);
         OnFileDownloaded?.Invoke(updInfo.FilePath[fileIndex]);
         lock (thislock)
         {
             wipCount--;
             if (wipCount == 0)
             {
                 OnUpdateDone?.Invoke();
             }
         }
     }
     catch (Exception ex)
     {
         OnExceptionRaised?.Invoke(ex);
     }
 }
        public void DownloadFile(string url, string folder)
        {
            string pathToNewFolder = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), folder);

            Directory.CreateDirectory(pathToNewFolder);

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                string pathToNewFile = Path.Combine(pathToNewFolder, "com.companyname.TestZXing.apk");
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception ex)
            {
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                }
            }
        }
 private void Completed(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         if (OnFileDownloaded != null)
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
         }
     }
     else
     {
         if (OnFileDownloaded != null && !_urls.Any())
         {
             OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
         }
         else if (OnFileDownloaded != null)
         {
             DownloadFile();
         }
     }
 }
Exemple #27
0
        public async Task Sync(bool restore = false, bool reloadAll = false)
        {
            if (Status == FileDownloadStatus.InProgress)
            {
                return;
            }

            cts    = new CancellationTokenSource();
            Status = FileDownloadStatus.InProgress;

            files = await GetUrlListAsync(restore, reloadAll);

            OnFileListLoaded?.Invoke(files);

            await files
            .Where(x => x.DownloadStatus != FileDownloadStatus.Done)
            .ForEachAsyncConcurrent(async x =>
            {
                await _fileDownloader.Download(x, cts.Token, (f) => { OnFileDownloaded?.Invoke(); });
            }, NumberOfTasks);

            Status = cts.IsCancellationRequested
                ? FileDownloadStatus.Cancelled
                : FileDownloadStatus.Done;

            await SaveFileList(files);

            var filesToDelete =
                (from fileName in new DirectoryInfo(_fileService.FilesDir).GetFiles().Select(x => x.Name).ToArray()
                 from f in files.Where(x => x.Name == fileName && x.DownloadStatus == FileDownloadStatus.Done).DefaultIfEmpty()
                 where f == null
                 select fileName).ToArray();

            foreach (var fileName in filesToDelete)
            {
                _fileService.DeleteFile(fileName);
            }

            OnAllFilesDownloaded?.Invoke();
        }
        private void OnDownloadCompleted(bool hasError, string filePath, ActionFileDownload action, object data, bool isSlient)
        {
            var fileSaved = !hasError;

            if (hasError)
            {
                if (!isSlient)
                {
                    UserDialogs.Instance.Alert("Download Error");
                }
            }
            else
            {
                // TODO do open file here
                switch (action)
                {
                case ActionFileDownload.Open:
                    FileService.OpenFile(filePath);
                    break;

                case ActionFileDownload.Download:
                    UserDialogs.Instance.Toast("File is Downloaded");
                    var notification = new Plugin.LocalNotification.NotificationRequest
                    {
                        NotificationId = 101,
                        Title          = "An item has downloaded ",
                        Description    = "Click to view more detail",
                        ReturningData  = filePath
                    };
                    NotificationCenter.Current.Show(notification);
                    break;
                }
            }

            if (OnFileDownloaded != null)
            {
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(fileSaved, filePath, data));
            }
        }
Exemple #29
0
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    if (OnFileDownloaded != null)
                    {
                        OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
                    }
                }
                else
                {
                    MessagingCenter.Send <App, string>((App)Xamarin.Forms.Application.Current, "downloadComplete", "true");
                    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                    StrictMode.SetVmPolicy(builder.Build());

                    Java.IO.File file      = new Java.IO.File(pathToNewFolder, Path.GetFileName(urlGet));
                    string       extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                    string       mimeType  = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                    Intent       intent    = new Intent(Intent.ActionView);
                    intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
                    intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/vnd.android.package-archive");

                    intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                    Forms.Context.StartActivity(Intent.CreateChooser(intent, "Your title"));

                    if (OnFileDownloaded != null)
                    {
                        OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
        public void DownloadFile(string url, string folder, string fileName)
        {
            string pathToNewFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);

            Directory.CreateDirectory(pathToNewFolder);

            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += Completed;
                webClient.Headers.Add("PrivateFileName", fileName);
                string pathToNewFile = Path.Combine(pathToNewFolder, fileName);
                webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Error on file download: {ex}");
                if (OnFileDownloaded != null)
                {
                    OnFileDownloaded.Invoke(this, new DownloadEventArgs(false, fileName));
                }
            }
        }