1

I am trying to upload large file on Google drive performing Resumable upload.

Here is the code flow

Step 1 : Creating file on Google Drive using Drive service and initiating the resumable upload session using put request

    String fileID = _DriveService.Files.Insert(googleFileBody).Execute().Id;

    //Initiating resumable upload session

    String UploadUrl = null;

    String _putUrl = "https://www.googleapis.com/upload/drive/v2/files/" + fileID  + "?uploadType=resumable";  

    HttpWebRequest httpRequest =  (HttpWebRequest)WebRequest.Create(_putUrl);    
    httpRequest.Headers["Authorization"] = "Bearer " + AccessToken;
    httpRequest.Method = "PUT";
    requestStream = httpRequest.GetRequestStream();
    _webResponse = (HttpWebResponse)httpRequest.GetResponse();

    if (_webResponse.StatusCode == HttpStatusCode.OK)
     {
         //Getting response OK
           UploadUrl = _webResponse.Headers["Location"].ToString();          
     }

Step 2 : Uploading chunks to using UploadUrl . The byte array is in multiple of 256kb and call to this function is in the loop for every chunk

    private void AppendFileData(byte[] chunk)
    {
      try
        {
          HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(UploadUrl);
          httpRequest.ContentLength = chunk.Length;
          httpRequest.Headers["Content-Range"] = "bytes " + startOffset + "-" + endOffset+ "/" + sourceFileSize;
          httpRequest.ContentType= MimeType;
          httpRequest.Method = "PUT";

         MemoryStream stream =new MemoryStream(chunk);

          using (System.IO.Stream requestStream = httpRequest.GetRequestStream())
                    {
                        stream.CopyTo(requestStream);
                        requestStream.Flush();
                        requestStream.Close();
                    }

          HttpWebResponse httpResponse = (HttpWebResponse)(httpRequest.GetResponse()); // Throws exception as 
          //System.Net.WebException: The remote server returned an error: (308) Resume Incomplete.
         //at System.Net.HttpWebRequest.GetResponse()
         // There is no data getting appended to file
        // Still executing the append for remaining chunks

        }
      catch(System.Net.WebException ex)
        {
        }
    }

For my last chunk which is not multiple of 256KB I am getting error as

System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse()

What I am doing wrong in this code? Please suggest. Thanks in advance

Mayuresh.

Mayuresh
  • 423
  • 1
  • 6
  • 24

1 Answers1

0

Try checking if the last chunk is passing the correct size and not the entire array as stated in this forum. Ali has stated in that forum that "one potential issue is this: if you are sending a byte array which is half empty for the last request (i.e. the buffer has been read in less than the chunk size)." Here is a sample implementation of resumable upload. Hope this helps.

Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • 1
    I figured it out. Issue was due to "Content-Range". The endoffset for last request should be less than one by total file size – Mayuresh May 15 '17 at 11:53