Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getfilebyserverrelativeurl return corrupted file for .docx file #3024

Open
kaushalbcactus opened this issue Apr 30, 2024 · 3 comments
Open

Comments

@kaushalbcactus
Copy link

Major Version

3.x

Minor Version Number

3.19.0

Target environment

NodeJS

Additional environment details

I am trying to fetch file from sharepoint but it is returning corrupted file.
Related to my issue Similar issue raised earlier by me. I was trying to analyse and realised that it worked for txt files but returned corrupted file for .docx type of file.

Expected or Desired Behavior

Calling function should return non corrupted file in buffer

Observed Behavior

I am getting corrupted file for .docx file

Steps to Reproduce

import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/files";
import "@pnp/sp/folders";

const sp = spfi(...);
const buffer: ArrayBuffer = await sp.web.getFileByServerRelativePath("/sites/dev/documents/Test.txt").getBuffer();

Please try below files:-

  1. Test_qerty (1).docx - I tried getText() and returned garbled text
  2. Test.txt - I tried getText() and returned proper text
@patrick-rodgers
Copy link
Member

this is correct, docx files are not text files and getText renders the raw textual content of the file. It does not extract text or understand docx or any other format.

@kaushalbcactus
Copy link
Author

Thanks @patrick-rodgers but retrieved buffer data issue still exists for me

  1. Please try both methods and check getserverrelativeurl returns different data

  2. `getFile: async function (headers, fileurl) {

     let responsePromise;
     if (Object.keys(headers).length) {
         const cred = {
         clientId: 'abc',
         clientSecret: 'abc',
         realm: 'abc'
         }
         const authtoken = await sprequest.getAuth('https://abc', cred);
         headers['Authorization'] = authtoken.headers.Authorization;
         responsePromise = got(fileurl, { headers });
     } else {
         responsePromise = got(fileurl);
     }
     const bufferPromise = responsePromise.buffer();
     const [response, buffer] = await Promise.all([responsePromise, bufferPromise]);
     return buffer;
    

    }`

  3. Use below method

  4. `import { spfi } from "@pnp/sp";
    import "@pnp/sp/webs";
    import "@pnp/sp/files";
    import "@pnp/sp/folders";

const sp = spfi(...);
const buffer: ArrayBuffer = await sp.web.getFileByServerRelativePath("/sites/dev/documents/Test.txt").getBuffer();
const flatArray = response.flat(2);
const obj = common.getResponseArray(flatArray, 'Obj');
const buffer = Buffer.from(obj);`

  1. file
    Test_qerty (1).docx

We are passing this buffer data to another service where buffer from got works but buffer from this data fails and both buffer returns different length of data.

@kaushalbcactus
Copy link
Author

Hi can anybody please assist in this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants