ASP.NET MVC: FileStreamResult returning too many bytes?

I am making an MVC controller method call. The return type is FileStreamResult. In this method, I create an image as an array of bytes. I create a MemoryStream by passing an array of bytes in the constructor. Then I return a new FileStreamResult object with a memory stream object and "image / png" in the constructor, since my image is PNG.

public FileStreamResult GetImage()
    {
        ImageModel im = new ImageModel();
        var image = im.GetImageInByteArray();
        var stream = new MemoryStream(image);
        return new FileStreamResult(stream, "image/png");
    }

Now that I get a stream of requests from this call, I just use the following method to convert the stream string to an array of bytes to view the image. However, after that, everything will end with more than 100 positions in my byte array than when I return from the MVC controller method call.

public static byte[] ConvertStringToBytes(string input)
    {
           MemoryStream stream = new MemoryStream();

           using (StreamWriter writer = new StreamWriter(stream))
           {
             writer.Write(input);
             writer.Flush();
           }

         return stream.ToArray();
   }

, "GetImageInByteArray()" "byte [256]". MVC, , , "byte [398]";

- -, GetImage(), , ?

, . .

?

+3
3

MVC , ASP.NET, HTTP, () , .

:

Response Code
Content-Length
Content-Type

HTTP ( HTTP).

, , , , , ( cookie?).

? - ?

+2

? -, .

:

return (new System.Text.UTF8Encoding()).GetBytes(input);

0

I really appreciate your help from David, but I decided to just go the other way. I created a transfer object to just save the byte array as a property. I serialize this, then pass the object across, deserialize on the opposite end, and I have an array of bytes, as it were.

I was still confused that the HTTP request was bound to a memory stream that was transferred, but I just went a different route.

0
source

All Articles