当前位置: 首页>>代码示例>>C#>>正文


C# Stream.CopyToAsync方法代码示例

本文整理汇总了C#中System.IO.Stream.CopyToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.CopyToAsync方法的具体用法?C# Stream.CopyToAsync怎么用?C# Stream.CopyToAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Stream的用法示例。


在下文中一共展示了Stream.CopyToAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WriteStreamAsync

 public static async Task WriteStreamAsync(string path, Stream contents)
 {
     using (var writer = File.Create(path))
     {
         await contents.CopyToAsync(writer).ConfigureAwait(false);
     }
 }
开发者ID:EmmittJ,项目名称:PoESkillTree,代码行数:7,代码来源:FileEx.cs

示例2: SendAsync

 public static async Task SendAsync(this HttpWebRequest request, Stream input)
 {
     using (var stream = await request.GetRequestStreamAsync())
     {
         await input.CopyToAsync(stream);
     }
 }
开发者ID:nokia6102,项目名称:jasily.cologler,代码行数:7,代码来源:HttpWebRequestExtensions.cs

示例3: SaveFile

 public async Task SaveFile(string name, Stream stream)
 {
     var fullPath = GetFullPath(name);
     using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write))
     {
         await stream.CopyToAsync(fs).ConfigureAwait(false);
         await stream.FlushAsync().ConfigureAwait(false);
         await fs.FlushAsync().ConfigureAwait(false);
     }
 }
开发者ID:tomasjurasek,项目名称:infrastructure,代码行数:10,代码来源:FileSystemStorageFolder.cs

示例4: ConvertToRandomAccessStream

        public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(Stream source)
        {
            Stream streamToConvert = null;
            
            if (!source.CanRead)
                throw new Exception("The source cannot be read.");

            if (!source.CanSeek)
            {
                var memStream = new MemoryStream();
                await source.CopyToAsync(memStream);
                streamToConvert = memStream;
            }
            else
            {
                streamToConvert = source;
            }

            var reader = new DataReader(streamToConvert.AsInputStream());
            streamToConvert.Position = 0;
            await reader.LoadAsync((uint) streamToConvert.Length);
            var buffer = reader.ReadBuffer((uint) streamToConvert.Length);

            var randomAccessStream = new InMemoryRandomAccessStream();
            var outputStream = randomAccessStream.GetOutputStreamAt(0);
            await outputStream.WriteAsync(buffer);
            await outputStream.FlushAsync();

            return randomAccessStream;
        }
开发者ID:bendewey,项目名称:LeadPro,代码行数:30,代码来源:ImageStreamConverter.cs

示例5: FromStream

		internal static Bitmap FromStream(Stream stream)
		{
		    Bitmap bitmap = null;

		    Task.Run(async () =>
		    {
		        using (var raStream = new InMemoryRandomAccessStream())
		        {
		            await stream.CopyToAsync(raStream.AsStream());
		            var decoder = await BitmapDecoder.CreateAsync(raStream);
		            var pixelData = await decoder.GetPixelDataAsync();

		            var width = (int)decoder.OrientedPixelWidth;
		            var height = (int)decoder.OrientedPixelHeight;
		            const PixelFormat format = PixelFormat.Format32bppArgb;
		            var bytes = pixelData.DetachPixelData();

                    bitmap = new Bitmap(width, height, format);
                    var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, format);
                    Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
                    bitmap.UnlockBits(data);
                }
		    }).Wait();

		    return bitmap;
		}
开发者ID:hungdluit,项目名称:aforge,代码行数:26,代码来源:Bitmap.Store.cs

示例6: CompressAsync

        public static async Task<Stream> CompressAsync(CompressionType type, Stream original)
        {            
            using (var ms = new MemoryStream())
            {
                Stream compressedStream = null;

                if (type == CompressionType.deflate)
                {
                    compressedStream = new DeflateStream(ms, CompressionMode.Compress);
                }
                else if (type == CompressionType.gzip)
                {
                    compressedStream = new GZipStream(ms, CompressionMode.Compress);
                }

                if (type != CompressionType.none)
                {
                    using (compressedStream)
                    {
                        await original.CopyToAsync(compressedStream);
                    }

                    //NOTE: If we just try to return the ms instance, it will simply not work
                    // a new stream needs to be returned that contains the compressed bytes.
                    // I've tried every combo and this appears to be the only thing that works.

                    byte[] output = ms.ToArray();
                    return new MemoryStream(ms.ToArray());
                }

                //not compressed
                return original;
            }
        }
开发者ID:eByte23,项目名称:Smidge,代码行数:34,代码来源:Compressor.cs

示例7: CopyStreamToByteBuffer

        public async Task<byte[]> CopyStreamToByteBuffer(Stream stream)
        {
            var memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);

            return memoryStream.ToArray();
        }
开发者ID:mathiasnohall,项目名称:Servanda,代码行数:7,代码来源:StreamHandler.cs

示例8: Resize

        public async Task<ResizeResult> Resize(string fileName, Stream stream)
        {
            string wwwroot = this.env.WebRootPath;

            string folderpath = Path.Combine(wwwroot, "dl");
            if (!this.fileSystemHelper.isDirectoryExists(folderpath))
            {
                this.fileSystemHelper.CreateDirectory(folderpath);
            }

            string filePath = Path.Combine(folderpath, fileName);
            using (var fs = this.fileSystemHelper.CreateFile(filePath))
            {
                await stream.CopyToAsync(fs);
            }

            string rawUri = string.Format(CultureInfo.InvariantCulture,
                "{0}://{1}/dl/{2}",
                this.httpContextAccessor.HttpContext.Request.Scheme,
                this.httpContextAccessor.HttpContext.Request.Host,
                fileName);
            return new ResizeResult()
            {
                UriActualSize = rawUri,
                UriResizedSize = await this.resizer.Resize(rawUri, Constants.MaxHeight, Constants.MaxWide)
            };
        }
开发者ID:shrimpy,项目名称:AspMVCFileUploadSample,代码行数:27,代码来源:LocalFileProvider.cs

示例9: AddFileAsync

 public async Task AddFileAsync(string outputPath, Stream sourceStream)
 {
     using (var writeStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
     {
         await sourceStream.CopyToAsync(writeStream);
     }
 }
开发者ID:robbert229,项目名称:Scaffolding,代码行数:7,代码来源:DefaultFileSystem.cs

示例10: AddAsync

 public async Task AddAsync(MobileServiceFile file, Stream source)
 {
     using (var target = await this.fileSystem.CreateAsync(GetFileName(file)))
     {
         await source.CopyToAsync(target);
     }
 }
开发者ID:Azure,项目名称:azure-mobile-apps-net-files-client,代码行数:7,代码来源:FileSystemStorageProvider.cs

示例11: ComputeHashAsync

        /// <exception cref="System.ArgumentException">Stream \data\ must be readable.;data</exception>
        /// <inheritdoc />
        public virtual async Task<byte[]> ComputeHashAsync(Stream data)
        {
            if (!data.CanRead)
                throw new ArgumentException("Stream \"data\" must be readable.", "data");


            if (!data.CanSeek && RequiresSeekableStream)
            {
                byte[] buffer;

                using (var ms = new MemoryStream())
                {
                    await data.CopyToAsync(ms)
                        .ConfigureAwait(false);

                    buffer = ms.ToArray();
                }

                // Use non-async because all of the data is in-memory
                return ComputeHashInternal(
                    new ArrayData(buffer));
            }


            return await ComputeHashAsyncInternal(new StreamData(data))
                .ConfigureAwait(false);
        }
开发者ID:PragmaticIT,项目名称:Data.HashFunction,代码行数:29,代码来源:HashFunctionAsyncBase.cs

示例12: ProcessContentStream

 internal static async Task ProcessContentStream(Stream contentStream, Dictionary<string, List<string>> options)
 {
     if (contentStream != null)
     {
         string fileName = options.ContainsKey(Constants.FILE) ? options[Constants.FILE][0] : null;
         if (fileName != null)
         {
             using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None, 1012 * 1024, true))
             {
                 await contentStream.CopyToAsync(stream);
                 Console.WriteLine("The partstudio has been downloaded to {0}", fileName);
             }
         }
         else
         {
             long bufferLength = (contentStream.Length > Constants.MAX_FILE_LENGTH_TO_PRINT_OUT) ? Constants.MAX_FILE_LENGTH_TO_PRINT_OUT : contentStream.Length;
             byte[] buffer = new byte[bufferLength];
             contentStream.Read(buffer, 0, (int)contentStream.Length);
             Console.WriteLine(Encoding.ASCII.GetString(buffer));
             if (contentStream.Length > Constants.MAX_FILE_LENGTH_TO_PRINT_OUT)
             {
                 Console.WriteLine("...");
             }
         }
     }
     else
     {
         Console.WriteLine("Download failed");
     }
 }
开发者ID:onshape-public,项目名称:app-windows-sample,代码行数:30,代码来源:Utils.cs

示例13: UploadFile

        public async Task<UploadDto> UploadFile(string fileName, string contentType, Stream stream)
        {
            var uploadDirectoryPath = Path.Combine(hostingEnvironment.WebRootPath, settings.LocalFileSystemStoragePath);

            string randomFile = Path.GetFileNameWithoutExtension(fileName) +
                                "_" +
                                Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName);


            if (!Directory.Exists(uploadDirectoryPath))
            {
                Directory.CreateDirectory(uploadDirectoryPath);
            }


            var targetFile = Path.GetFullPath(Path.Combine(uploadDirectoryPath, randomFile));

            using (var destinationStream = File.Create(targetFile))
            {
                await stream.CopyToAsync(destinationStream);
            }


            var result = new UploadDto
            {
                FileExtn = Path.GetExtension(fileName),
                FileName =  fileName,
                Url = GetFullUrl(settings.LocalFileSystemStorageUriPrefix, randomFile)
            };

            return result;
        }
开发者ID:kshyju,项目名称:ProjectPlanningTool,代码行数:32,代码来源:LocalFileSystemStorageHandler.cs

示例14: UploadFile

        public async Task<UploadResult> UploadFile(string fileName, string contentType, Stream stream)
        {
            ApplicationSettings settings = _settingsFunc();

            // Randomize the filename everytime so we don't overwrite files
            string randomFile = Path.GetFileNameWithoutExtension(fileName) +
                                "_" +
                                Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName);


            if (!Directory.Exists(settings.LocalFileSystemStoragePath))
            {
                Directory.CreateDirectory(settings.LocalFileSystemStoragePath);
            }

            // REVIEW: Do we need to validate the settings here out of paranoia?
            
            string targetFile = Path.GetFullPath(Path.Combine(settings.LocalFileSystemStoragePath, randomFile));

            using (FileStream destinationStream = File.Create(targetFile))
            {
                await stream.CopyToAsync(destinationStream);
            }


            var result = new UploadResult
            {
                Url = GetFullUrl(settings.LocalFileSystemStorageUriPrefix, randomFile),
                Identifier = randomFile
            };

            return result;
        }
开发者ID:QuinntyneBrown,项目名称:JabbR,代码行数:33,代码来源:LocalFileSystemStorageHandler.cs

示例15: ReadAllBytesAsync

 private async Task<byte[]> ReadAllBytesAsync(Stream stream)
 {
     using (var memoryStream = new MemoryStream())
     {
         await stream.CopyToAsync(memoryStream);
         return memoryStream.ToArray();
     }
 }
开发者ID:MrTortoise,项目名称:graphviz,代码行数:8,代码来源:RendererTests.cs


注:本文中的System.IO.Stream.CopyToAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。