0

I am tying to upload large files(1 GB+) to Google Drive using GoogleDrive API. My code works fine with smaller files. But when it comes to larger files error occurs.

Error occurs in the code part where the the file is converted into byte[].

byte[] data = System.IO.File.ReadAllBytes(filepath); 

Out of memory exception is thrown here.

Devi
  • 15
  • 1
  • 6
  • 2
    The error you receive is explicit enough. What are you asking us exactly? – Frédéric Hamidi Oct 07 '15 at 13:05
  • What I want exactly is, to upload large files to google drive using google drive sdk....I searched a lot , but couldn't find a solution that works.. – Devi Oct 12 '15 at 11:33

2 Answers2

5

Probably you followed developers.google suggestions and you are doing this

byte[] byteArray = System.IO.File.ReadAllBytes(filename);
MemoryStream stream = new MemoryStream(byteArray);
try {
  FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
  request.Upload();

I have no idea why the suggest to put the whole file in a byte array and then create a MemoryStream on it. I think that a better way is this:

using(var stream = new System.IO.FileStream(filename,
                                            System.IO.FileMode.Open, 
                                            System.IO.FileAccess.Read))
{
    try 
    {
       FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
       request.Upload();
       .
       .
       .
}
Matteo Umili
  • 3,412
  • 1
  • 19
  • 31
0

I use next code and works fine whith a file > 1gb

using (var stream = new System.IO.FileStream(_uploadFile, System.IO.FileMode.Open, System.IO.FileAccess.Read))       
{              
    try
    {
        FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, GetMimeType(_uploadFile));
        request.SupportsTeamDrives = true;
        request.Upload();
        var file = request.ResponseBody;
        string fileId = file.Id;
    
        request = null;
    }
    catch (Exception ex)
    {
               
    }

    stream.Dispose();
    body = null;
}
moken
  • 3,227
  • 8
  • 13
  • 23
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit](https://stackoverflow.com/posts/76238423/edit) to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken May 19 '23 at 08:08