google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

"onUploadProgress" not working

Open VidulHB opened this issue 2 years ago • 0 comments

Hi, i am trying to track the progress of file upload with the onUploadProgress function but its not working

  const bufferStream = new stream.PassThrough();
  bufferStream.end(fileObject.buffer);
  const Filename = `${fileObject.fieldname + "_" + date + "_" + fileObject.originalname}`;

  const fileSize = fileObject.buffer.length;

  const media = {
    mimeType: fileObject.mimeType,
    body: bufferStream,
  };

  const fileMetadata = {
    name: Filename,
    parents: ['1HuCYguIQhi0OFcnhbcr2JwPwJzsVeWzJ'],
  };

  const upload = drive.files.create(
    {
      resource: fileMetadata,
      media: media,
      fields: 'id,name',
    },
    {
      onUploadProgress: evt => {
        const progress = (evt.bytesRead / fileSize) * 100;
        readline.clearLine(process.stdout, 0);
        readline.cursorTo(process.stdout, 0);
        process.stdout.write(`${Math.round(progress)}% complete`);
      },
    }
  );
  try {
    const response = await upload;
    console.log('File uploaded successfully');
    res.status(200).send(`${Filename}`);
  } catch (error) {
    console.error('Error uploading file:', error);
    res.status(500).send('Error uploading file');
  }
};

i found the official example for this but even that doesn't work https://github.com/googleapis/google-api-nodejs-client/blob/main/samples/drive/upload.js

Issue: It returns the progress only when it reaches 100%

VidulHB avatar Jun 29 '23 13:06 VidulHB