ASP.NET MVC FileStreamResult 未按预期工作

编程入门 行业动态 更新时间:2024-10-25 15:25:10
本文介绍了ASP.NET MVC FileStreamResult 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码,我从任何非必要的行中删除了这些代码,以保留最小的可重现情况.我期望它返回图像,但它没有.据我所知,它返回一个空文件:

I have the following code which I stripped out of any non-essential lines to leave the minimun reproducable case. What I expect is for it to return the image, but it doesn't. As far as I can see it returns an empty file:

public ActionResult Thumbnail(int id) { var question = GetQuestion(db, id); var image = new Bitmap(question.ImageFullPath); MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); return new FileStreamResult(stream, "image/jpeg"); }

你能找出这段代码有什么问题吗?在调试器中,我可以看到流的大小在增长,因此它似乎正在获取数据,尽管我无法验证它是正确的数据.我不知道如何调试 FileStreamResult 本身.

Can you identify what's wrong with this code? In the debugger I can see that the stream grows in size so it seems to be getting the data although I haven't been able to verify it's the correct data. I have no idea how to debug the FileStreamResult itself.

推荐答案

需要插入

stream.Seek(0, SeekOrigin.Begin);

调用后

Image.Save()

这会将流倒回到已保存图像的开头.否则,流将位于流的末尾,并且不会向接收器发送任何内容.

This will rewind the stream to the beginning of the saved image. Otherwise the stream will be positioned at the end of the stream and nothing is sent to the receiver.

更多推荐

ASP.NET MVC FileStreamResult 未按预期工作

本文发布于:2023-11-17 03:30:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未按   工作   NET   ASP   FileStreamResult

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!