BetterCms.Core.Services.Storage.FileSystemStorageService.DownloadObject C# (CSharp) Method

DownloadObject() public method

public DownloadObject ( Uri uri ) : BetterCms.Core.Services.Storage.DownloadResponse
uri System.Uri
return BetterCms.Core.Services.Storage.DownloadResponse
        public DownloadResponse DownloadObject(Uri uri)
        {
            CheckUri(uri);

            var downloadResponse = new DownloadResponse();
            downloadResponse.Uri = uri;

            using (FileStream writeStream = new FileStream(uri.LocalPath, FileMode.Open, FileAccess.Read))
            {
                downloadResponse.ResponseStream = new MemoryStream();
                writeStream.CopyTo(downloadResponse.ResponseStream);
                downloadResponse.ResponseStream.Seek(0, SeekOrigin.Begin);

                writeStream.Flush();
                writeStream.Close();
            }

            return downloadResponse;
        }

Usage Example

 public void Should_Download_Object_Successfully()
 {
     string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString().Replace("-", string.Empty) + ".tmp");
     const string content = "test content";
     try
     {
         File.WriteAllText(path, content);
         FileSystemStorageService storageService = new FileSystemStorageService();
         Uri downloadUri = new Uri(path);
         var download = storageService.DownloadObject(downloadUri);
         Assert.AreEqual(download.Uri, downloadUri);
         using (TextReader reader = new StreamReader(download.ResponseStream))
         {
             string contentFromStream = reader.ReadToEnd();
             Assert.AreEqual(content, contentFromStream);
         }
     }
     finally
     {
         if (File.Exists(path))
         {
             File.Delete(path);
         }
     }
 }
All Usage Examples Of BetterCms.Core.Services.Storage.FileSystemStorageService::DownloadObject