mvc 提供文件流服务的真正方式是什么?

发布于 2024-12-21 17:16:45 字数 1005 浏览 2 评论 0原文

我有一个错误的方法让 ASP.NET MVC 提供文件流服务。它看起来像这样:

    public void SlideThumbnail(Guid id, int? width, int? height)
    {
        /*make the thumbnail code here*/

        using (Bitmap thumbnail = imageThumb.Generate(path))
        {
            var msOutput = new MemoryStream();

            thumbnail.Save(msOutput, ImageFormat.Png);
            Response.ContentType = "image/png";
            msOutput.WriteTo(Response.OutputStream);
        }
    }

这非常有效。我尝试修改代码以使用 base.File() 代替。就像这样:

    public ActionResult SlideThumbnail(Guid id, int? width, int? height)
    {
        /*make the thumbnail code here*/

        using (Bitmap thumbnail = imageThumb.Generate(path))
        {
            var msOutput = new MemoryStream();

            thumbnail.Save(msOutput, ImageFormat.Png);
            return base.File(msOutput, "image/png");
        }
    }

但这一切似乎实际上并没有起到任何作用。我没有收到任何错误,但也没有收到任何图像:-(

那么我如何以 MVC 方式实现这一点呢?

I have an incorrect way to make asp.net mvc serve a filestream. It looks like this:

    public void SlideThumbnail(Guid id, int? width, int? height)
    {
        /*make the thumbnail code here*/

        using (Bitmap thumbnail = imageThumb.Generate(path))
        {
            var msOutput = new MemoryStream();

            thumbnail.Save(msOutput, ImageFormat.Png);
            Response.ContentType = "image/png";
            msOutput.WriteTo(Response.OutputStream);
        }
    }

This works super duper. I tried modifying the code to use base.File() instead. Like this:

    public ActionResult SlideThumbnail(Guid id, int? width, int? height)
    {
        /*make the thumbnail code here*/

        using (Bitmap thumbnail = imageThumb.Generate(path))
        {
            var msOutput = new MemoryStream();

            thumbnail.Save(msOutput, ImageFormat.Png);
            return base.File(msOutput, "image/png");
        }
    }

But all this doesn't seem to actually do anything. I don't get any errors, but I also don't get any image :-(

So how do i accomplish this the MVC way?

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

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

发布评论

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

评论(1

情话难免假 2024-12-28 17:16:45

我们想通了。我只需要补充一点:

thumbnail.Save(msOutput, ImageFormat.Png);
msOutput.Seek(0, SeekOrigin.Begin); // <--- this line
return base.File(msOutput, "image/png"); 

我们都很好。

We figured it out. I just needed to add:

thumbnail.Save(msOutput, ImageFormat.Png);
msOutput.Seek(0, SeekOrigin.Begin); // <--- this line
return base.File(msOutput, "image/png"); 

and we're all good.

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