简体   繁体   English

将 MemoryStream 复制到 FileStream 并保存文件?

[英]Copy MemoryStream to FileStream and save the file?

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).但是当我尝试将MemoryStream复制到FileStream以保存文件时, fileStream未填充且文件长度为 0bytes(空)。

Here is my code这是我的代码

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

You used the outStream when saving the file using the imageFactory .您在使用outStream保存文件时使用了imageFactory That function populated the outStream .该函数填充了outStream While populating the outStream the position is set to the end of the populated area.在填充outStream ,位置被设置为填充区域的末端。 That is so that when you keep on writing bytes to the steam, it doesn't override existing bytes.这样,当您继续将字节写入 Steam 时,它不会覆盖现有字节。 But then to read it (for copy purposes) you need to set the position to the start so you can start reading at the start.但是要阅读它(用于复制目的),您需要将位置设置为开头,以便您可以从头开始阅读。

If your objective is simply to dump the memory stream to a physical file (eg to look at the contents) - it can be done in one move:如果您的目标只是将内存流转储到物理文件(例如查看内容) - 可以一步完成:

System.IO.File.WriteAllBytes(@"C:\\filename", memoryStream.ToArray());

No need to set the stream position first either, since the .ToArray() operation explicitly ignores that, as per @BaconBits comment below https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream.toarray?view=netframework-4.7.2 .也不需要先设置流位置,因为 .ToArray() 操作明确忽略了这一点,根据https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream下面的@BaconBits 评论.toarray?view=netframework-4.7.2

Another alternative to CopyTo is WriteTo . CopyTo另一种替代方法是WriteTo

Advantage:优势:

No need to reset Position.无需重置位置。

Usage:用法:

outStream.WriteTo(fileStream);                

Function Description: 功能说明:

Writes the entire contents of this memory stream to another stream.将此内存流的全部内容写入另一个流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM