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


C# this.CopyToAsync方法代码示例

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


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

示例1: ReadAsFileStreamAsync

        public static Task ReadAsFileStreamAsync(this HttpContent content, string path, bool overwrite = true)
        {
            if (!overwrite && File.Exists(path))
            {
                throw new InvalidOperationException(string.Format("File {0} already exists!", path));
            }

            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
                return content.CopyToAsync(fileStream).ContinueWith(
                    task =>
                    {
                        fileStream.Close();
                    });
            }
            catch (Exception e)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }

                throw e;
            }
        }
开发者ID:anshox,项目名称:SyncReader,代码行数:27,代码来源:HttpContentExtensions.cs

示例2: ReadAsFileAsync

        /// <summary>
        /// Reserved for internal use.
        /// </summary>
        public static Task ReadAsFileAsync(this HttpContent content, string filename, bool overwrite)
        {
            string pathname = Path.GetFullPath(filename);
            if (!overwrite && File.Exists(filename))
            {
                throw new InvalidOperationException(string.Format("File {0} already exists.", pathname));
            }

            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None);

                return content.CopyToAsync(fileStream).ContinueWith(_ => fileStream.Close());
            }
            catch
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }

                throw;
            }
        }
开发者ID:moinahmed,项目名称:BingAds-dotNet-SDK,代码行数:28,代码来源:HttpContentExtensions.cs

示例3: AsRandomAccessStreamAsync

        public async static Task<IRandomAccessStream> AsRandomAccessStreamAsync(this Stream stream)
        {
            Stream streamToConvert = null;

            if (!stream.CanRead)
            {
                throw new Exception("Cannot read the source stream-");
            }
            if (!stream.CanSeek)
            {
                MemoryStream memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream);
                streamToConvert = memoryStream;
            }
            else
            {
                streamToConvert = stream;
            }

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

            InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
            IOutputStream outputstream = randomAccessStream.GetOutputStreamAt(0);
            await outputstream.WriteAsync(buffer);
            await outputstream.FlushAsync();

            return randomAccessStream;
        }
开发者ID:uwe-e,项目名称:BSE.Tunes,代码行数:31,代码来源:StreamExtensions.cs

示例4: ReadAllBytesAsync

 public static Task<byte[]> ReadAllBytesAsync(this Stream stream, CancellationToken cancellationToken = default(CancellationToken)) {
     if (stream == null) {
         throw new ArgumentNullException("stream");
     }
     var temp = new MemoryStream(stream.CanSeek ? (int)stream.Length : 0);
     return stream.CopyToAsync(temp, null, cancellationToken)
                  .Then(() => temp.ToArray());
 }
开发者ID:castenguyen,项目名称:SPA-with-AngularJS,代码行数:8,代码来源:StreamExtensions.cs

示例5: SaveAsAsync

        public static async Task SaveAsAsync(this IFormFile uploadFile, string filePath)
        {
            using (var stream = System.IO.File.Create(filePath))
            {
                await uploadFile.CopyToAsync(stream); 
            }

        }
开发者ID:ChavFDG,项目名称:Stolons,代码行数:8,代码来源:FileHelper.cs

示例6: ReadBytesAsync

 /// <summary>
 /// Copies the stream to a byte array.
 /// </summary>
 /// <param name="input">Stream to read from</param>
 /// <returns>Byte array copy of a stream</returns>
 public static async Task<byte[]> ReadBytesAsync(this Stream input)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         await input.CopyToAsync(ms);
         return ms.ToArray();
     }
 }
开发者ID:WhoCarrot,项目名称:WopiHost,代码行数:13,代码来源:Extensions.cs

示例7: ToDataUri

 /// <summary>
 /// Converts a stream to a data URL.
 /// </summary>
 /// <param name="stream">The stream which provides the content for the data URI.</param>
 /// <param name="mediaType">The media type of the stream.</param>
 /// <returns>A string that contains the data uri of the stream's content.</returns>
 public static async Task<string> ToDataUri(this Stream stream, string mediaType)
 {
     // copy to memory stream and convert the bytes to a base64 encoded string
     using (var ms = new MemoryStream())
     {
         await stream.CopyToAsync(ms);
         return ms.ToArray().ToDataUri(mediaType);
     }
 }
开发者ID:Cimpress-MCP,项目名称:dotnet-core-httputils,代码行数:15,代码来源:DataUriConversion.cs

示例8: ToArrayAsync

        /// <summary>
        ///     Asynchronously writes the stream contents to a byte array.
        /// </summary>
        /// <param name="stream">
        ///     The stream to convert to byte array.
        /// </param>
        /// <returns>
        ///     A byte array of the <paramref name="stream"/>'s content.
        /// </returns>
        public async static Task<byte[]> ToArrayAsync(this Stream stream)
        {
            using (var memoryStream = new MemoryStream())
            {
                await stream.CopyToAsync(memoryStream);

                return memoryStream.ToArray();
            }
        }
开发者ID:mdabbagh88,项目名称:UniversalImageLoader,代码行数:18,代码来源:StreamExtension.cs

示例9: SaveToFileAsync

 /// <summary>
 /// Saves a stream to a file.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="pathToFile">The path to file.</param>
 public static async Task SaveToFileAsync(this Stream stream, string pathToFile)
 {
     Check.NotNull(stream, nameof(stream));
     Check.NotEmpty(pathToFile, nameof(pathToFile));
     using (Stream output = File.Open(pathToFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
     {
         await stream.CopyToAsync(output).ConfigureAwait(false);
     }
 }
开发者ID:Recognos,项目名称:Recognos.Core,代码行数:14,代码来源:FileUtilities.cs

示例10: ReadAsFileAsync

 public async static Task ReadAsFileAsync(this HttpContent content, string fileName)
 {
     string pathName = Path.GetFullPath(fileName);
     
     using(FileStream fileStream = new FileStream(pathName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
     {
         await content.CopyToAsync(fileStream);
     }
 }
开发者ID:jammerware,项目名称:bazam,代码行数:9,代码来源:HttpContentExtensions.cs

示例11: ToHttpResponseMessage

 public static async Task<HttpResponseMessage> ToHttpResponseMessage(this Stream stream)
 {
     var response = new HttpResponseMessage();
     var memoryStream = new MemoryStream();
     await stream.CopyToAsync(memoryStream);
     response.Content = new ByteArrayContent(memoryStream.ToArray());
     response.Content.Headers.Add("Content-Type", "application/http;msgtype=response");
     return await response.Content.ReadAsHttpResponseMessageAsync();
 }
开发者ID:prabirshrestha,项目名称:dotnet-test-httpclient,代码行数:9,代码来源:HttpResponseMessageExtensions.cs

示例12: GetBytesAsync

        public static async Task<byte[]> GetBytesAsync(this Stream stream)
        {
            Requires.NotNull(stream, "stream");

            using (var memoryStream = new MemoryStream())
            {
                await stream.CopyToAsync(memoryStream);
                return memoryStream.ToArray();
            }
        }
开发者ID:alekseysukharev,项目名称:proxy,代码行数:10,代码来源:StreamExtensions.cs

示例13: CopyAsync

 public static async Task<MemoryStream> CopyAsync(this Stream stream, CancellationToken cancellation)
 {
     MemoryStream buffer = new MemoryStream();
     if (stream.CanSeek && stream.Position != 0)
     {
         stream.Seek(0, SeekOrigin.Begin);
     }
     await stream.CopyToAsync(buffer, 4096, cancellation);
     buffer.Seek(0, SeekOrigin.Begin);
     return buffer;
 }
开发者ID:geffzhang,项目名称:Bolt,代码行数:11,代码来源:StreamExtensions.cs

示例14: ReadAsFileAsync

        public static async Task ReadAsFileAsync(this HttpContent content, string filename, bool overwrite) {
            string pathname = Path.GetFullPath(filename);
            if (!overwrite && File.Exists(filename)) {
                throw new InvalidOperationException(string.Format("File {0} already exists.", pathname));
            }

            using (var fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None)) {
                await content.CopyToAsync(fileStream);
                fileStream.Close();
            }
        }
开发者ID:IIHS,项目名称:side-waffle,代码行数:11,代码来源:HttpContentExtensions.cs

示例15: CopyToAsync

        /// <summary>
        /// Asynchronously reads the bytes from a source stream and writes them to a destination stream.
        /// </summary>
        /// <remarks>
        /// <para>Copying begins at the current position in <paramref name="stream"/>.</para>
        /// </remarks>
        /// <param name="stream">The source stream.</param>
        /// <param name="destination">The stream to which the contents of the source stream will be copied.</param>
        /// <returns>A task that represents the asynchronous copy operation.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para>If <paramref name="stream"/> is <see langword="null"/>.</para>
        /// <para>-or-</para>
        /// <para>If <paramref name="destination"/> is <see langword="null"/>.</para>
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// <para>If <paramref name="stream"/> is disposed.</para>
        /// <para>-or-</para>
        /// <para>If <paramref name="destination"/> is disposed.</para>
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// <para>If <paramref name="stream"/> does not support reading.</para>
        /// <para>-or-</para>
        /// <para>If <paramref name="destination"/> does not support writing.</para>
        /// </exception>
        public static Task CopyToAsync(this Stream stream, Stream destination)
        {
            #if NET45PLUS
            if (stream == null)
                throw new ArgumentNullException("stream");

            // This code requires the `Stream` class provide an implementation of `CopyToAsync`. The unit tests will
            // detect any case where this results in a stack overflow.
            return stream.CopyToAsync(destination);
            #else
            return CopyToAsync(stream, destination, 16 * 1024, CancellationToken.None);
            #endif
        }
开发者ID:gitter-badger,项目名称:dotnet-threading,代码行数:37,代码来源:StreamExtensions.cs


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