Browse Source

Check stream CanSeek before change Position.

pull/8253/head
maliming 6 years ago
parent
commit
2e9b3e6d19
  1. 6
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs
  2. 15
      framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs

6
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs

@ -28,7 +28,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters
using (var stream = remoteStream.GetStream()) using (var stream = remoteStream.GetStream())
{ {
stream.Position = 0; if (stream.CanSeek)
{
stream.Position = 0;
}
await stream.CopyToAsync(context.HttpContext.Response.Body); await stream.CopyToAsync(context.HttpContext.Response.Body);
} }
} }

15
framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs

@ -9,7 +9,10 @@ namespace System.IO
{ {
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
stream.Position = 0; if (stream.CanSeek)
{
stream.Position = 0;
}
stream.CopyTo(memoryStream); stream.CopyTo(memoryStream);
return memoryStream.ToArray(); return memoryStream.ToArray();
} }
@ -19,7 +22,10 @@ namespace System.IO
{ {
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
stream.Position = 0; if (stream.CanSeek)
{
stream.Position = 0;
}
await stream.CopyToAsync(memoryStream, cancellationToken); await stream.CopyToAsync(memoryStream, cancellationToken);
return memoryStream.ToArray(); return memoryStream.ToArray();
} }
@ -27,7 +33,10 @@ namespace System.IO
public static Task CopyToAsync(this Stream stream, Stream destination, CancellationToken cancellationToken) public static Task CopyToAsync(this Stream stream, Stream destination, CancellationToken cancellationToken)
{ {
stream.Position = 0; if (stream.CanSeek)
{
stream.Position = 0;
}
return stream.CopyToAsync( return stream.CopyToAsync(
destination, destination,
81920, //this is already the default value, but needed to set to be able to pass the cancellationToken 81920, //this is already the default value, but needed to set to be able to pass the cancellationToken

Loading…
Cancel
Save