本文整理汇总了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);
}
}
示例2: SendAsync
public static async Task SendAsync(this HttpWebRequest request, Stream input)
{
using (var stream = await request.GetRequestStreamAsync())
{
await input.CopyToAsync(stream);
}
}
示例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);
}
}
示例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;
}
示例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;
}
示例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;
}
}
示例7: CopyStreamToByteBuffer
public async Task<byte[]> CopyStreamToByteBuffer(Stream stream)
{
var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
示例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)
};
}
示例9: AddFileAsync
public async Task AddFileAsync(string outputPath, Stream sourceStream)
{
using (var writeStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
{
await sourceStream.CopyToAsync(writeStream);
}
}
示例10: AddAsync
public async Task AddAsync(MobileServiceFile file, Stream source)
{
using (var target = await this.fileSystem.CreateAsync(GetFileName(file)))
{
await source.CopyToAsync(target);
}
}
示例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);
}
示例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");
}
}
示例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;
}
示例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;
}
示例15: ReadAllBytesAsync
private async Task<byte[]> ReadAllBytesAsync(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}