如预期ASP.NET MVC FileStreamResult不工作 [英] ASP.NET MVC FileStreamResult not working as intended

查看:177
本文介绍了如预期ASP.NET MVC FileStreamResult不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,我去掉了所有非必要行离开最低限度的可再现情况。我期望的是它返回的形象,但事实并非如此。至于我可以看到它返回一个空文件:

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

您可以找出什么不对的code?在调试器,我可以看到流生长在大小,因此它似乎是获取数据,虽然我还没有能够验证它的正确性。我不知道如何调试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不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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