Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. Angular exceljs adding an image gives the error: u.readFile is not a function at exceljs.min.js:3:449768 Angular application

Angular exceljs adding an image gives the error: u.readFile is not a function at exceljs.min.js:3:449768 Angular application

Scheduled Pinned Locked Moved JavaScript
javascriptdata-structureshelplounge
2 Posts 2 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephen Holdorf
    wrote on last edited by
    #1

    I am developing in Angular 12 with the exceljs package. I am trying to add an image to a Workbook and the TS below produces and throws the client side exception: Error: Uncaught (in promise): TypeError: u.readFile is not a function TypeError: u.readFile is not a function at exceljs.min.js:3:449768 Here is the code I am calling:

    let imageSrc = '\\assets\\images\\graph.jpeg'
    
    var imageId1 = workbook.addImage(
      {
        filename: imageSrc,
        extension: 'jpeg',
      });
    
    workbook.xlsx.writeBuffer().then((data) => {
      let blob = new Blob(\[data\], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      fs.saveAs(blob, 'Random Number Generator.xlsx');
    });
    
    Richard DeemingR 1 Reply Last reply
    0
    • S Stephen Holdorf

      I am developing in Angular 12 with the exceljs package. I am trying to add an image to a Workbook and the TS below produces and throws the client side exception: Error: Uncaught (in promise): TypeError: u.readFile is not a function TypeError: u.readFile is not a function at exceljs.min.js:3:449768 Here is the code I am calling:

      let imageSrc = '\\assets\\images\\graph.jpeg'
      
      var imageId1 = workbook.addImage(
        {
          filename: imageSrc,
          extension: 'jpeg',
        });
      
      workbook.xlsx.writeBuffer().then((data) => {
        let blob = new Blob(\[data\], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        fs.saveAs(blob, 'Random Number Generator.xlsx');
      });
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Based on this GitHub issue[^], it looks like addImage can't use the path of an image on the server to generate a file on the client. The suggested workaround is to load the image as a buffer. The code in the issue uses axios, but you could use the fetch API instead[^]:

      const imageSrc = '/assets/images/graph.jpeg';
      const response = await fetch(imageSrc);
      const buffer = await response.arrayBuffer();
      const imageId1 = workbook.addImage({
      buffer: imageBuffer.data,
      extension: 'jpg'
      });


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups