Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

MemoryStream Copy to ResponseStream failed

Open leixu2txtek opened this issue 8 years ago • 2 comments

ms

the

ms.Copyto(s)

is failed to response the image

var bytes = ms.ToArray();
s.Write(bytes, 0, bytes.Length);

this is ok to response the image.

Where I am wrong ?

  • Nancy version: 2.0.0-clinteastwood
  • Nancy host
    • [ ] ASP.NET
    • [x] OWIN
    • [ ] Self-Hosted
  • Environment (Operating system, version and so on): windows 10 14393.969
  • .NET Framework version: .net core 1.1

leixu2txtek avatar Apr 05 '17 10:04 leixu2txtek

Try this

public class ByteArrayResponse : Response { ///

/// Byte array response /// /// Byte array to be the body of the response</ param> /// Content type to use public ByteArrayResponse(byte[] body, string contentType = null) { this.ContentType = contentType ?? "application/octet-stream";

        this.Contents = stream =>
            {
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write(body);
                }
            };
    }
}

jchannon avatar Apr 05 '17 10:04 jchannon

@jchannon ok, thx.

leixu2txtek avatar Apr 06 '17 02:04 leixu2txtek