【发布时间】:2013-09-16 22:41:28
【问题描述】:
我不明白我在这里做错了什么。我生成了几个内存流,在调试模式下我看到它们已被填充。但是当我尝试将 MemoryStream 复制到 FileStream 以保存文件时,fileStream 未填充且文件长度为 0 字节(空)。
这是我的代码
if (file.ContentLength > 0)
{
var bytes = ImageUploader.FilestreamToBytes(file); // bytes is populated
using (var inStream = new MemoryStream(bytes)) // inStream is populated
{
using (var outStream = new MemoryStream())
{
using (var imageFactory = new ImageFactory())
{
imageFactory.Load(inStream)
.Resize(new Size(320, 0))
.Format(ImageFormat.Jpeg)
.Quality(70)
.Save(outStream);
}
// outStream is populated here
var fileName = "test.jpg";
using (var fileStream = new FileStream(Server.MapPath("~/content/u/") + fileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
outStream.CopyTo(fileStream); // fileStream is not populated
}
}
}
}
【问题讨论】:
-
您确定 outStream 包含图像数据吗?我认为 .Save(outStream); 中的问题你能发布调用堆栈吗?
-
@BassamAlugili 伙计,我检查了调试模式。它的人口。
outStream.Position = 0是问题所在,我需要设置它。
标签: c# file-upload filestream memorystream