Beispiel #1
0
        public void CopyToAsync_NullDestination_ThrowsArgumentNullException()
        {
            byte[] contentData = CreateSourceArray();
            var    content     = new ByteArrayContent(contentData);

            Assert.Throws <ArgumentNullException>(() => { Task t = content.CopyToAsync(null); });
        }
 public override string Render(string body = "")
 {
     if (!CheckSecurity("export"))
     {
         return(AccessDenied());
     }
     try
     {
         var filename = "SaberExport.zip";
         var content  = new ByteArrayContent(SaberZip.Export());
         content.Headers.ContentType                 = new MediaTypeHeaderValue("application/zip");
         content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
         content.Headers.ContentDisposition.FileName = filename;
         Context.Response.ContentLength              = content.Headers.ContentLength;
         Context.Response.ContentType                = "application/zip";
         Context.Response.StatusCode                 = 200;
         Context.Response.Headers.Add("Content-Disposition", "attachment; filename=" + filename);
         content.CopyToAsync(Context.Response.Body);
     }
     catch (Exception ex)
     {
         return(Error(ex.Message + "\n" + ex.StackTrace));
     }
     return("");
 }
Beispiel #3
0
        public async Task CopyToAsync_UseEmptySourceArray_NothingCopied()
        {
            var contentData = new byte[0];
            var content     = new ByteArrayContent(contentData, 0, 0);

            var destination = new MemoryStream();
            await content.CopyToAsync(destination);

            Assert.Equal(0, destination.Length);
        }
Beispiel #4
0
        public async Task CopyToAsync_UsePartialSourceArray_PartialContentCopied()
        {
            byte[] contentData = CreateSourceArray();
            var    content     = new ByteArrayContent(contentData, 3, 5);

            var destination = new MemoryStream();
            await content.CopyToAsync(destination);

            Assert.Equal(5, destination.Length);
            CheckResult(destination, 3);
        }
Beispiel #5
0
        public async Task CopyToAsync_UseWholeSourceArray_WholeContentCopied()
        {
            byte[] contentData = CreateSourceArray();
            var    content     = new ByteArrayContent(contentData);

            var destination = new MemoryStream();
            await content.CopyToAsync(destination);

            Assert.Equal(contentData.Length, destination.Length);
            CheckResult(destination, 0);
        }
        public void CopyToAsync()
        {
            byte[] b = { 4, 2 };

            var sc = new ByteArrayContent(b);

            var dest = new MemoryStream();
            var task = sc.CopyToAsync(dest);

            Assert.IsTrue(task.Wait(500));
            Assert.AreEqual(2, dest.Length, "#1");
        }
        public void CopyTo_Invalid()
        {
            var m = new MemoryStream();

            var sc = new ByteArrayContent(new byte[0]);

            try {
                sc.CopyToAsync(null);
                Assert.Fail("#1");
            } catch (ArgumentNullException) {
            }
        }
        /// <summary>
        /// Creates file response stream.
        /// </summary>
        /// <param name="targetStream">
        /// The target stream where the file stream is copied.
        /// </param>
        /// <param name="path">
        /// The path of the file to put in a stream.
        /// </param>
        private void CreateFileResponseStream(Stream targetStream, string path)
        {
            using (var fileStream = new FileStream(path, FileMode.Open))
            {
                var fileSize = fileStream.Length;
                var buffer   = new byte[(int)fileSize];
                fileStream.Read(buffer, 0, (int)fileSize);

                var content = new ByteArrayContent(buffer);

                // stream the multipart content to the request contents target stream
                content.CopyToAsync(targetStream);
            }

            System.IO.File.Delete(path);
        }