Raj
Raj

Reputation: 173

MongoDB 3.2 C# driver version 2.2.3.3 Gridfs Download large files more than 2gb

I am uploading files using the following code:

using (var s = File.OpenRead(@"C:\2gbDataTest.zip"))           

{

 var t = Task.Run<ObjectId>(() =>

{

return fs.UploadFromStreamAsync("2gbDataTest.zip", s);

});


 return t.Result;

}

//works for the files below 2gb

  var t1 = fs.DownloadAsBytesAsync(id);

            Task.WaitAll(t1);
            var bytes = t1.Result;

I am getting error

enter image description here

I am new to MongoDb and C#, can any one please show me how to download files greater than 2GB in size?

Upvotes: 1

Views: 1107

Answers (2)

Adam Comerford
Adam Comerford

Reputation: 21692

You are hitting the limit in terms of the size a byte array (kept in memory) download can be, so your only choice is to use a Stream instead like you are doing when you upload, something like (with a valid destination):

IGridFSBucket fs;
ObjectId id;
FileStream destination;

await fs.DownloadToStreamAsync(id, destination);

Upvotes: 2

Raj
Raj

Reputation: 173

//Just writing complete code for others, This will work ; //Thanks to "Adam Comerford"

var fs = new GridFSBucket(database);

using (var newFs = new FileStream(filePathToDownload, FileMode.Create))

{

//id is file objectId

var t1 = fs.DownloadToStreamAsync(id, newFs);

Task.WaitAll(t1);

newFs.Flush();

newFs.Close();

}

Upvotes: 0

Related Questions