0

So, I have an issue with my Fullstack app, in which I have "images" in my "log" database stored as bytes that contains data to my PNG images in my database. I however cannot load them into my front-end.

My Prisma Schema (in my front end, all the data is under "logs" so log.Title etc to display the title in React)

model log {
  id Int @id
  Title String @db.VarChar(255)
  Content String @db.Text
  images Bytes
  date String @db.VarChar(255)
}

My front-end where I am displaying the data

const CoinIntelLogLayout = ({ log }) => {

  const imageArray = new Uint8Array(log.images);
  const blob = new Blob([imageArray], { type: "image/png" });
  const blobUrl = URL.createObjectURL(blob);

  return (
    <div className='flex flex-col justify-center items-center bg-whiteColor rounded-xl shadow-xl shadow-cyan-500/50 w-[400px] h-[400px]'>
  <div className='flex flex-col justify-center items-start gap-4 w-[350px]'>
    <div className='flex flex-col justify-center items-start' >
      <h2 className='bg-gradient-to-r from-purplebrownColor to-yellowColor bg-clip-text text-transparent font-jostSemiBold text-3xl pb-1'>{log.Title}</h2>
      <div className='bg-gradient-to-r from-purplebrownColor to-yellowColor h-1 w-[80%]'></div>
      <h2 className='bg-gradient-to-r from-purplebrownColor to-yellowColor bg-clip-text text-transparent font-generalSansRegular text-md pt-1'>{log.Date}</h2>
    </div>
    <h2 className='font-generalSansRegular text-md text-blackishColor'>{log.Content}</h2>
    <img className='h-11 w-auto' src={blobUrl} alt='Update Image' draggable={false}></img>
  </div>
</div>
  )
}

I tried to use blob to get this done, but it didn't work. I am not sure how to fix this, as passing in {log.images} into the image src does nothing.

6
  • 1
    When you say "data to my PNG images", do you mean the actual image data? Generally what I've done is fetch images from a database via <img> tags directly or via new Image(), using an API that loads the image, sets the MIME header, and ships out the bytes. Commented Aug 8 at 18:21
  • I'm confused. Should have mentioned I'm still very much new to fullstack web dev. I believe my images are stored in mysql (the value of the images, so I assume it's actual data) as binary (bytes in prisma schema). I figured I could just get all the data via prisma client and then turn the data into something Javascript can read as a png (the blob code you see). I tried loading "log.images" itself in the image tag directly, but it loads no images. A bit confused about what you're suggesting. Commented Aug 8 at 19:29
  • It's possible to do that but somewhat complicated; I don't know if what I'm suggesting is easy with Prisma but it's easy with a plain web server. Commented Aug 8 at 20:21
  • Also it's not clear what images is in your function; there's no parameter with that name. Commented Aug 8 at 20:21
  • Apologies, I meant "log.images". Typo on my end Commented Aug 9 at 0:07

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.