Ejemplo n.º 1
0
        private async Task <bool> DownloadFile(StorageFile localFile, string path)
        {
            bool result = false;

            CachedFileManager.DeferUpdates(localFile);
            var _cts = new CancellationTokenSource();
            IProgress <WebDavProgress> progress = new Progress <WebDavProgress>(ProgressHandler);

            using (var randomAccessStream = await localFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                Stream targetStream = randomAccessStream.AsStreamForWrite();

                result = await client.Download(path, targetStream, progress, _cts.Token);
            }

            // Let Windows know that we're finished changing the file so
            // the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            await CachedFileManager.CompleteUpdatesAsync(localFile);

            return(result);
        }
Ejemplo n.º 2
0
        private async Task Download(ResourceInfo resourceInfo, NextcloudClient.NextcloudClient client, StorageFolder folder)
        {
            if (resourceInfo.ContentType == "dav/directory")
            {
                ResourceInfo = new ResourceInfo
                {
                    Name        = resourceInfo.Name + ".zip",
                    ContentType = "application/zip",
                    Path        = resourceInfo.Path
                };
            }
            else
            {
                ResourceInfo = resourceInfo;
            }

            var savePicker = new FileSavePicker();

            savePicker.FileTypeChoices.Add(ResourceInfo.ContentType, new List <string> {
                Path.GetExtension(ResourceInfo.Name)
            });
            savePicker.SuggestedFileName = ResourceInfo.Name;

            StorageFile localFile;

            if (folder != null)
            {
                try
                {
                    localFile = await folder.CreateFileAsync(
                        savePicker.SuggestedFileName,
                        CreationCollisionOption.GenerateUniqueName);
                }
                catch (FileNotFoundException)
                {
                    //this.textBlock.Text = "Error " + ex;
                    return;
                }
            }
            else
            {
                localFile = await savePicker.PickSaveFileAsync();
            }
            if (localFile == null)
            {
                return;
            }

            _currentFile = localFile;

            // Prevent updates to the remote version of the file until
            // we finish making changes and call CompleteUpdatesAsync.
            CachedFileManager.DeferUpdates(localFile);

            try
            {
                IProgress <WebDavProgress> progress = new Progress <WebDavProgress>(ProgressHandler);

                using (var randomAccessStream = await localFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var targetStream = randomAccessStream.AsStreamForWrite();

                    switch (resourceInfo.ContentType)
                    {
                    case "dav/directory":
                        await client.DownloadDirectoryAsZip(ResourceInfo.Path, targetStream, progress, _cts.Token);

                        break;

                    default:
                        await client.Download(ResourceInfo.Path + "/" + ResourceInfo.Name, targetStream, progress, _cts.Token);

                        break;
                    }
                }
            }
            catch (ResponseError e2)
            {
                ResponseErrorHandlerService.HandleException(e2);
            }

            // Let Windows know that we're finished changing the file so
            // the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            var status = await CachedFileManager.CompleteUpdatesAsync(localFile);

            if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
            {
                //this.textBlock.Text = "Path " + file.Name + " was saved.";
            }
            else
            {
                //this.textBlock.Text = "Path " + file.Name + " couldn't be saved.";
            }
        }