0

I know how to get the file size using REST API, basically this here:

https://tenant.sharepoint.com/sites/site_name/_api/web/GetFileByServerRelativePath(decodedurl='/sites/site_name/Lists/ListName/Attachments/1/Airbus_Pleiades_50cm_8bit_RGB_Yogyakarta.jpg')/Length

But for the love of me I can't figure out how to do this using PnP JS.

Any help would be appreciated.

The official doc is not much help here: https://pnp.github.io/pnpjs/sp/files/

I will use the RAW REST API for now but really would like to know how to use PnP SP for that

2
  • Check this example where they are getting knownLength from file stream. Let me know if this works for you. Commented Oct 31, 2021 at 15:44
  • @GaneshSanap I am not using Node JS its a standard WP that runs in the browser so I can't use streaming like that.
    – Idra
    Commented Oct 31, 2021 at 15:54

3 Answers 3

2

You could get the file length like this:

  sp.web
    .getFileByServerRelativePath("/sites/test/Shared Documents/a.txt")
    .get()
    .then(file=>{
              console.log(file.Length)
          });
0

Try using this:

const file = await sp.web.getFileByServerRelativeUrl(fileUrl).getBuffer();
const fileLength = file.byteLength;
console.log(fileLength);
1
  • 1
    Yes that works, but it has the downside of downloading the file. We have a 40MB file that we are testing with and it is not acceptable to wait that long just to get the file size.
    – Idra
    Commented Oct 31, 2021 at 17:58
0

PnP.js might not have every single REST endpoint implemented. However, you can still call any REST end endpoint you want. You don't need PnP.js wrapper for everything. For example, this is how you can get the file size using SPHttpClient:

import { sp, SPHttpClient } from "@pnp/sp/presets/all";

(async () => {
  var url = "https://contoso.sharepoint.com/_api/web/GetFileByServerRelativePath(decodedurl='/Lists/Clients/Attachments/1/Converted_Demo_Solution_1_0_0_1.zip')/Length"
  const client = new SPHttpClient();
  const response = await client.get(url, {
    headers: {
      accept: "application/json"
    }
  });

  const json = await response.json();
  console.log(`file size is ${json.value}`)

})().catch(console.log)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.