复制的MemoryStream对FILESTREAM并保存文件? [英] Copy MemoryStream to FileStream and save the file?

查看:1424
本文介绍了复制的MemoryStream对FILESTREAM并保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白我在做什么错在这里。我生成夫妇内存流,并在调试模式我看到他们被填充。但是,当我试图复制的MemoryStream 的FileStream 以保存文件 FILESTREAM 不填充和文件是0bytes长(空的)。

I don't understand what I'm doing wrong here. I generate couple of memory streams and in debug-mode I see that they are populated. But when I try to copy MemoryStream to FileStream in order to save the file fileStream is not populated and file is 0bytes long (empty).

下面是我的代码

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
            }
        }
    }
}


推荐答案

您需要重置复制前的数据流中的位置。

You need to reset the position of the stream before copying.

outStream.Position = 0;
outStream.CopyTo(fileStream);

这篇关于复制的MemoryStream对FILESTREAM并保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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