简体   繁体   English

将大文件转换为byte []以存储在SQL Server(C#)中

[英]Convert large files in byte[] to store in SQL Server (C#)

I have a file with a size of approx 1.6 GB. 我有一个大小约为1.6 GB的文件。 I am aware of storing large file in SQL Server . 我知道在SQL Server存储大文件 But I have a requirement to generate the blob for this large file. 但是我需要为此大文件生成Blob。

I am using following code to generate the byte[] for the large file: 我正在使用以下代码为大型文件生成byte[]

string filePath = Server.MapPath(filename);
string filename = Path.GetFileName(filePath);

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

Byte[] bytes = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();

It is throwing 它在扔

An exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll中发生类型'System.OutOfMemoryException'的异常,但未在用户代码中处理

I want to know that How can I convert this large file to byte[] ? 我想知道如何将这个大文件转换为byte[]

You SHOULD not do that, either read the file using chunks or in you case if you want to upload it to SharePoint, just connect the two streams together and SharePoint library will do the rest, ex: 您不应该这样做,或者使用块读取文件,或者如果您想将其上传到SharePoint,只需将两个流连接在一起,SharePoint库将完成其余工作,例如:

FileStream fileStream = File.OpenRead(filename);
SPFile spfile = theLibrary.Files.Add(fileName, fileStream, true);

This is done in SharePoint server side object model, the same can be done with CSOM 这是在SharePoint服务器端对象模型中完成的,与CSOM相同。

     Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fileStream, true);

Basically, you can't, not with a byte array. 基本上,您不能这样做,而不是使用字节数组。

You can do it for objects larger than 2GB, if you are: 如果满足以下条件,则可以对大于2GB的对象执行此操作:

1) Running in 64bit mode on a 64 bit system. 1)在64位系统上以64位模式运行。 32bit apps cannot address memory bigger than 1.5GB. 32位应用程序无法处理大于1.5GB的内存。

2) Are running .NET Framework V4.5 or greater. 2)正在运行.NET Framework V4.5或更高版本。 And

3) Have set gcAllowVeryLargeObjects in your app.config: gcAllowVeryLargeObjects Element 3)在app.config中设置了gcAllowVeryLargeObjects: gcAllowVeryLargeObjects元素

But ... array indexers are still limited to an integer - so you can't do this with byte arrays as your array index would be too big. 但是...数组索引器仍然限于整数-因此您不能对字节数组执行此操作,因为您的数组索引太大。

You can do it with a stream (~8TB is allowed) but not with an array. 您可以使用流(允许约8TB)执行此操作,但不能使用数组。

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

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