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


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

  • Check this example where they are getting knownLength from file stream. Let me know if this works for you. 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
    Oct 31, 2021 at 15:54

3 Answers 3


You could get the file length like this:

    .getFileByServerRelativePath("/sites/test/Shared Documents/a.txt")

Try using this:

const file = await sp.web.getFileByServerRelativeUrl(fileUrl).getBuffer();
const fileLength = file.byteLength;
  • 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
    Oct 31, 2021 at 17:58

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}`)


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.