Mongodb C# Driver GridFS : Appending a Chunk to an Existing File

  Kiến thức lập trình

Is there a way to append a chunk to an existing file using GridFS? The API I am working with does not allow me to upload the entirety of a file to the database, it has to be one chunk at a time. If there is no way by using GridFS, is there something else I can do?

I tried to use the method OpenUploadStream(string, GridFSUploadOptions = null) to open a stream of the file, but it creates a new file in the fs.files collection.

Here is the code :

    public Exception StoreSingleChunk(string fileHash, byte[] data)
    {
      Exception occuredEx = null;
      int attempts = 0;
      do
      {
        try
        {
          GridFSUploadOptions uploadOptions = new GridFSUploadOptions
          {
            DisableMD5 = true
          };

          using (var uploadStream = gridFSBucket.OpenUploadStream(fileHash, uploadOptions))
          {
            uploadStream.Write(data, 0, data.Length);
            uploadStream.Close();
          }
          occuredEx = null;
        }
        catch (Exception ex)
        {
          occuredEx = ex;
        }
        finally
        {
          if (occuredEx != null)
            Task.Delay(500).Wait();
          attempts++;
        }
      } while (occuredEx != null && attempts < _nbAttempts);
      return occuredEx;
    }

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT