You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

ishan_PGT's avatar
ishan_PGT
Helpful | Level 5
4 years ago

DropBox sdk error in chunked upload “cannot access a closed file”

So I am using the below code to ChukUpload a 150MB file and I am getting an error at

var byteRead = fileStream.Read(buffer, 0, (int)ChunkSize);

the exception says its an "cannot access a closed file.". The first chunk is uploading fine but when I enter the loop

"for (ulong idx = 0; idx < numChunks; idx++)"  2nd time I am getting the error at fileStream.Read(buffer, 0, (int)ChunkSize);

 

 

using(var fileStream = File.Open(Path.Combine("downloads", title), FileMode.Open)) {
  Console.WriteLine("chunked Upload");
  //ChunkUpload(remotePath, fileStream, (int)ChunkSize,dbx);
  ulong numChunks = (ulong) Math.Ceiling((double) fileStream.Length / (int) ChunkSize);
  byte[] buffer = new byte[(int) ChunkSize];
  string sessionId = null;
  for (ulong idx = 0; idx < numChunks; idx++) {
    var byteRead = fileStream.Read(buffer, 0, (int) ChunkSize);

    using(var memStream = new MemoryStream(buffer, 0, byteRead)) {
      if (idx == 0) {
        try {
          var result = dbx.Files.UploadSessionStartAsync(false, body: fileStream).Result;
          sessionId = result.SessionId;
          Console.WriteLine(sessionId);

        } catch (Exception ex) {
          Console.WriteLine(ex.Message);
        }
      } else {
        var cursor = new UploadSessionCursor(sessionId, (ulong)(int) ChunkSize * idx);

        if (idx == numChunks - 1) {
          FileMetadata fileMetadata = dbx.Files.UploadSessionFinishAsync(cursor, new CommitInfo(remotePath, mode: WriteMode.Overwrite.Instance), memStream).Result;
          Console.WriteLine(fileMetadata.PathDisplay);
        } else {
          dbx.Files.UploadSessionAppendV2Async(cursor, false, memStream).Wait();
        }
      }
      //}
    }
  }
}

 

  • I found my mistake I was using the main filestream instead of the chunked memory stream in the 1st iteration.

  • ishan_PGT's avatar
    ishan_PGT
    Helpful | Level 5

    I found my mistake I was using the main filestream instead of the chunked memory stream in the 1st iteration.

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API

Make connections with other developers

797 PostsLatest Activity: 4 days ago
199 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!