ASP.NET MVC:FileStreamResult返回太多字节吗? [英] ASP.NET MVC: FileStreamResult returning too many bytes?

查看:69
本文介绍了ASP.NET MVC:FileStreamResult返回太多字节吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");
    }

现在,当我从此调用获取请求流时,我只是在使用以下方法将流字符串转换为字节数组以查看图像.但是,完成所有这些操作后,与从MVC控制器方法调用返回时相比,我在字节数组中最终得到100个以上的位置.

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();
   }

例如,在调用"GetImageInByteArray()"之后,我有了"byte [256]".但是,当它从该MVC调用返回并通过第二种方法转换响应字符串时,我最终得到"byte [398]";

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]";

编辑问题 我对GetImage()控制器方法提出的Web请求与我所假设收到的Web请求之间是否存在某种区分?

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?

推荐答案

当MVC操作返回结果时,它将通过ASP.NET管道,该管道附加HTTP头,以便请求者(浏览器)可以理解该怎么做.响应.

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

或其他任意数量的自定义或典型HTTP标头(更多HTTP标头).

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

如果我正确理解了您的问题,那么您会将整个响应从操作转换为字节,因此,自然地,您还将转换标头以及请求可能返回的其他任何内容(cookies?).

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?

这篇关于ASP.NET MVC:FileStreamResult返回太多字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆