cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We recently received a question asking for advice on how to set up a Dropbox account for an elderly family member. This prompted us to think about the different ways we can make Dropbox more accessible in general, learn about it and add your tips right over here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
1
Ask
2
Comments

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

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

ishan_PGT
Helpful | Level 5

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();
        }
      }
      //}
    }
  }
}

 

1 Accepted Solution

Accepted Solutions

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

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.

View solution in original post

1 Reply 1

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

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.

Who's talking

Top contributors to this post

  • User avatar
    ishan_PGT Helpful | Level 5
What do Dropbox user levels mean?
Need more support?