本文整理汇总了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;
}
}
示例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;
}
}
示例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;
}
示例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());
}
示例5: SaveAsAsync
public static async Task SaveAsAsync(this IFormFile uploadFile, string filePath)
{
using (var stream = System.IO.File.Create(filePath))
{
await uploadFile.CopyToAsync(stream);
}
}
示例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();
}
}
示例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);
}
}
示例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();
}
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例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();
}
}
示例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;
}
示例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();
}
}
示例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
}