The*_*Sky 13 c# asp.net-mvc asp.net-mvc-3
我有一个简单的表单,将图像上传到数据库.使用控制器操作,然后可以回送图像(我已经硬编码为此代码使用jpegs):
public class ImagesController : Controller
{
[HttpPost]
public ActionResult Create(HttpPostedFileBase image)
{
var message = new MessageItem();
message.ImageData = new byte[image.ContentLength];
image.InputStream.Read(message.ImageData, 0, image.ContentLength);
this.session.Save(message);
return this.RedirectToAction("index");
}
[HttpGet]
public FileResult View(int id)
{
var message = this.session.Get<MessageItem>(id);
return this.File(message.ImageData, "image/jpeg");
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用,直接浏览图像(例如/images/view/1)正确显示图像.但是,我注意到当FireBug打开时,我遇到了一个可爱的错误:
图像损坏或截断:data:image/jpeg; base64,/ f39 ...(后跟图像的base64表示).
此外,在Chrome开发者工具中:
资源解释为Document但使用MIME类型image/jpeg进行传输.
我检查了要返回的标头.以下是发送回浏览器的标头示例.没有什么看起来不寻常(可能是Cache-Control?):
Cache-Control private, s-maxage=0
Content-Type image/jpeg
Server Microsoft-IIS/7.5
X-AspNetMvc-Version 3.0
X-AspNet-Version 4.0.30319
X-SourceFiles =?UTF-8?B?(Trimmed...)
X-Powered-By ASP.NET
Date Wed, 25 May 2011 23:48:22 GMT
Content-Length 21362
Run Code Online (Sandbox Code Playgroud)
另外,我想我会提到我在IIS Express上运行它(甚至在Cassini上测试过相同的结果).
奇怪的是,图像显示正确,但控制台告诉我不然.理想情况下,我不想忽视这些错误.最后,为了进一步增加混淆,当作为图像(例如<img src="/images/view/1" />)引用时,不会发生错误.
编辑:没有上述任何操作,可以完全重现:
public class ImageController : Controller
{
public FileResult Test()
{
// I know this is directly reading from a file, but the whole purpose is
// to return a *buffer* of a file and not the *path* to the file.
// This will throw the error in FireBug.
var buffer = System.IO.File.ReadAllBytes("PATH_TO_JPEG");
return this.File(buffer, "image/jpeg");
}
}
Run Code Online (Sandbox Code Playgroud)
感谢大家的帮助。我知道这对于这个问题来说将是一个非常反气候的结局,但我能够“解决”这个问题。我尝试使用相同的浏览器/firebug 版本从另一台机器构建代码。奇怪的是,没有出现任何错误。当我回到另一台机器(清除所有缓存,甚至重新安装浏览器/firebug)时,它仍然收到错误。更奇怪的是,当我访问其他网站时,Chrome/Firefox 现在都显示错误。
再次感谢大家的建议!
| 归档时间: |
|
| 查看次数: |
1411 次 |
| 最近记录: |