pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
764 stars 304 forks source link

addChunked error: TypeError: body.stream(...).pipe is not a function #3094

Closed ibrahim-amer closed 3 months ago

ibrahim-amer commented 4 months ago

What version of PnPjs library you are using

4.x

Minor Version Number

3

Target environment

NodeJS

Additional environment details

I'm using NestJs.

Question/Request

Hi there,

I am encountering an issue when trying to upload a file in chunks using the addChunked method from PnP.js in a NestJS application. Specifically, I am trying to convert a buffer to a Blob and use it for the chunked upload, but I keep getting a TypeError: body.stream(...).pipe is not a function

async uploadFile(
      siteUrl: string,
      listId: string,
      serverRelativeUrl: string,
      folderPath: string,
      fileName: string,
      orignalFile: Express.Multer.File,
      userName: string,
      password: string,
      metadata: any,
    ): Promise<boolean> {
      try {
        console.log('uploadFile');
        await bootstrap(sp, { username: userName, password: password }, siteUrl);
        let fileBuffer = orignalFile.buffer;

        let list: List = null;
        if (listId.length !== 36) {
          console.log('createFoldersRecursive: getByTitle');
          list = await sp.web.lists.getByTitle(listId);
        } else {
          console.log('createFoldersRecursive: getById');
          list = await sp.web.lists.getById(listId);
        }
        const fullFolderPath = `${serverRelativeUrl}/${folderPath}`;
        await sp.web.getFolderByServerRelativeUrl(fullFolderPath);
        //convert buffer to file

        console.log('filebuffer length', fileBuffer.length);
        const fileBlob = new Blob([
          new Uint8Array(fileBuffer, 0, fileBuffer.length),
        ]);

        // Attempt to use File constructor (results in ReferenceError: File is not defined)

        const file = await list.rootFolder.files.addChunked(
          fileName,
          fileBlob,
          (data) => {
            console.log(
              'progress ' +
                data.blockNumber +
                ' Stage ' +
                data.stage +
                ' chunkSize: ' +
                data.chunkSize,
            );
          },
          true,
          5000000,
        );

        console.log('file after upload = ', file);
        let item = await file.file.getItem();

        console.log('uploadFile: after adding file');

        const metadataObj = JSON.parse(metadata);
        await item.update(metadataObj);
        for (const key in metadataObj) {
          if (metadataObj.hasOwnProperty(key)) {
            await item.update({ [key]: metadataObj[key] });
          }
        }

        console.log('uploadFile: after assigning metadata');
        return true;
      } catch (error) {
        console.log('uploadFile error', error);
        return false;
      }
    }

Please help. I've been strugling with this issue for over a week.

bcameron1231 commented 4 months ago

Hi. If you're doing this in Nestjs that means you're doing this server side on node? Can you try uploading the files using a stream instead

https://pnp.github.io/pnpjs/sp/files/#adding-a-file-using-nodejs-streams

bcameron1231 commented 3 months ago

Closing this due to inactivity. If you continue to have issues please open a new issue, link to this issue, and provide any additional details available. Thanks!

github-actions[bot] commented 3 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.