ASP.NET MVC:FileStreamResult 返回太多字节?

发布于 2024-10-21 19:39:12 字数 1157 浏览 5 评论 0原文

我正在调用 MVC 控制器方法。 返回类型是FileStreamResult。 在此方法中,我以字节数组的形式创建图像。 我正在创建一个 MemoryStream,在构造函数中传递字节数组。 然后,我在构造函数中返回一个新的 FileStreamResult 对象,其中包含内存流对象和“image/png”,因为我的图像是 PNG。

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

现在,当我从此调用中获取请求流时,我只需使用以下方法将流字符串转换为字节数组即可查看图像。 然而,在这一切完成之后,我的字节数组中的位置比从 MVC 控制器方法调用返回时多了 100 多个位置。

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() 控制器方法发出的 Web 请求与我假设收到的请求之间是否存在某种分歧?

我假设我从该调用中收到的是图像字节数组的内存流。 这就是为什么我只是将其转换回字节数组。

我的假设是错误的吗?

I'm making a call to an MVC controller method.
The return type is FileStreamResult.
In this method I'm creating an image in the form of a byte array.
I'm creating a MemoryStream, passing the byte array in the constructor.
I'm then returning a new FileStreamResult object with the memory stream object and "image/png" in the constructor, as my image is a PNG.

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

Now, when I get the request stream from this call, I'm simply using the following method to convert the stream string into a byte array to view the image.
However, after this is all done, I end up with 100+ more 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();
   }

For example, after the "GetImageInByteArray()" call, I have "byte[256]".
But when it returns from that MVC call and I convert the response string by putting it through the second method, I end up with "byte[398]";

EDIT QUESTION
Is there some kind of divide between the web request I'm making to the GetImage() controller method and what I assume I'm receiving?

I assume what I'm receiving from that call is the memory stream of the image byte array.
This is why I'm simply converting that back to a byte array.

Is my assumption here wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-10-28 19:39:12

当 MVC 操作返回结果时,它会通过附加在 HTTP 标头上的 ASP.NET 管道,以便请求者(浏览器)可以了解如何处理响应。

对于图像,这些标头可能包括:

Response Code
Content-Length
Content-Type

或任何其他数量的自定义或典型 HTTP 标头(更多 HTTP 标头< /a>)。

如果我正确理解你的问题,你会将操作的整个响应转换为字节,所以很自然,你还将转换标头和请求可能返回的任何其他内容(cookie?)。

您想通过反序列化请求来实现什么目的?你想测试一些东西吗?

When an MVC action returns a result, it goes through the ASP.NET pipeline which tacks on HTTP headers so that the requestor (browser) can understand what to do with the response.

For an image, these headers might include:

Response Code
Content-Length
Content-Type

Or any other number of custom or typical HTTP headers (Some more HTTP Headers).

If I understand your question correctly, you're converting the entire response from your action to bytes, so naturally, you'll also be converting the headers and anything else the request might return (cookies?).

What are you trying to accomplish with deserialization of the request? Are you trying to test something?

摇划花蜜的午后 2024-10-28 19:39:12

你的字符串是如何编码的?在我看来,这是编码的问题。

尝试将此作为您的方法代码:

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

问候

How is your string encoded ? In my opinion it is a problem with encoding.

Try this as your method code:

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

Greetings

七堇年 2024-10-28 19:39:12

我真的很感谢你对大卫的帮助,但我决定走另一条路。
我创建了一个传输对象来简单地将字节数组作为属性保存。
我序列化它,然后传递对象,在另一端反序列化,我就得到了原来的字节数组。

我仍然对 HTTP 请求附加到正在传递的内存流上的内容感到困惑,但我只是走了另一条路。

I really appreciate your help Dawid, but I decided to simply go another way.
I created a transfer object to simply hold the byte array as a property.
I serialize this, then pass the object across, deserialize on the opposite end and I have my byte array as it was.

I still am confused on what the HTTP request was tacking onto the memory stream that was being passed, but I just went another route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文