从文件流到内存流的转换速度很慢 [英] Coversion from filestream to memorystream slow

查看:432
本文介绍了从文件流到内存流的转换速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将文件流转换为内存流,这需要花费太多时间。

I am trying to convert filestream to memorystream using the below code which is taking too much of time.

  {

      byte [] buffer = new byte [1024 * 64];

      int nread = 0;

      while((nread = fileInfo.Stream.Read(buffer,0,buffer.Length))> 0)

      {

          memory.Write(buffer,0,nread);

      }¥b $ b      memory.Seek(0,SeekOrigin.Begin);

      docByteArray = memory.ToArray();

      docBase64String = Convert.ToBase64String(docByteArray);

  }

 {
      byte[] buffer = new byte[1024 * 64];
      int nread = 0;
      while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
      {
          memory.Write(buffer, 0, nread);
      }
      memory.Seek(0, SeekOrigin.Begin);
      docByteArray = memory.ToArray();
      docBase64String = Convert.ToBase64String(docByteArray);
  }

我使用csom从SharePoint获取文件流。下面是代码示例:

I get the filestream from SharePoint using csom. Below is the code sample:

  fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(spContext,fileRef.ToString());

 fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(spContext, fileRef.ToString());

关于如何掌握它的任何建议。

Any suggestions on how to make it master.

推荐答案

您一次读取64K字节。根据文件的大小,这可能需要很长时间。最重要的是,你正在使用一个内存流,它将分配一个更大的数组本身。如果你需要将一个流复制到另一个流,那么
考虑使用
CopyTo
代替。根据源流,这可能需要一段时间。如果你看起来部分向下发布

这里
然后你会看到一个扩展方法基本上就是这个。

You are reading 64K bytes at a time. Depending upon the size of the file this could take a long time. On top of that you're using a memory stream which is going to be allocating an ever larger array itself. If you need to copy one stream to another then consider using CopyTo instead. Depending upon the source stream this may take a while anyway. If you look part way down the SO posting here then you'll see an extension method that does basically this.

为什么要将文件复制到内存中?看起来你正在尝试对文件进行base64编码,但通常不需要。一旦完成,你没有提到你打算用数组做什么,但大多数支持base64
数组的调用也有一个重载流。您是否想看看是否有空位? 

Why are you copying the file into memory to begin with? It looks like you're trying to base64 encode the file but oftentimes that isn't necessary. You didn't mention what you intend to do with the array once you're done but most calls that support base64 arrays also have an overload for streams. Have you looked to see if one is available? 


这篇关于从文件流到内存流的转换速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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