Beispiel #1
0
        /// <summary>
        /// Reads the contents of a text file and displays the results in a TextBox.
        /// </summary>
        /// <param name="_selectedFileObject">The file selected in the ListBox.</param>
        /// <returns>A Boolean value that indicates whether the text file was successfully read.</returns>
        internal async Task <object[]> ReadTextFileAsync(string selectedItemID)
        {
            string fileContents = string.Empty;

            object[] results = new object[] { fileContents, false };

            try
            {
                // Make sure we have a reference to the SharePoint client
                var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");

                // Get a handle on the selected item.
                IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID);
                IFileFetcher thisFileFetcher = thisItemFetcher.ToFile();
                var          myFile          = await thisFileFetcher.ExecuteAsync();

                // Check that the selected item is a .txt file.
                if (!myFile.Name.EndsWith(".txt") && !myFile.Name.EndsWith(".xml"))
                {
                    results[0] = string.Empty;
                    results[1] = false;
                    return(results);
                }

                Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File;

                // Download the text file and put the results into a string. This results in a call to the service.
                using (Stream stream = await file.DownloadAsync())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        results[0] = await reader.ReadToEndAsync();

                        results[1] = true;
                    }
                }
            }
            catch (NullReferenceException)
            {
                results[1] = false;
            }
            catch (ArgumentException)
            {
                results[1] = false;
            }

            return(results);
        }
        async Task<StorageFile> DownloadFileAsync(File file)
        {
            StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;

            StorageFile invoice =
                await temporaryFolder.CreateFileAsync(file.Name, 
                Windows.Storage.CreationCollisionOption.ReplaceExisting);

            var stream = await file.DownloadAsync();                

            using (var reader = new DataReader(stream.AsInputStream()))
            {
                await reader.LoadAsync((uint)stream.Length);
                var buffer = new byte[(int)stream.Length];
                reader.ReadBytes(buffer);
                await FileIO.WriteBytesAsync(invoice, buffer);
            }

            return invoice;
        }
        internal async Task <bool> DownloadFileAsync(string selectedItemID)
        {
            var results = false;

            try
            {
                // Make sure we have a reference to the SharePoint client
                var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");

                // Get a handle on the selected item.
                IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID);
                IFileFetcher thisFileFetcher = thisItemFetcher.ToFile();
                var          myFile          = await thisFileFetcher.ExecuteAsync();

                Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File;

                // Download the file and put the results into a string. This results in a call to the service.
                using (Stream stream = await file.DownloadAsync())
                {
                    // TODO: save file to users locker
                    // Create a new file. If the file already exists, it will be overwritten.
                    using (var fileStream = new FileStream(@"C:\hosting\WebApp1\Downloads\" + myFile.Name, FileMode.Create, FileAccess.Write))
                    {
                        stream.CopyTo(fileStream);
                    }
                }

                results = true;
            }
            catch (Exception exc)
            {
                throw new Exception("Error occured while downloading file:  ", exc);
            }

            return(results);
        }