Profile avatar

Chizi Victor

Software developer and unabashed nerd. Based in Port Harcourt, Nigeria. Building my own ideas, helping you launch yours.

Snippets

Feb 25, 2024

Temporary Image Url

Generate imageUrl for preview from image file input

export function getImageData(event: ChangeEvent<HTMLInputElement>) {
  // FileList is immutable, so we need to create a new one
  const dataTransfer = new DataTransfer();

  // Add newly uploaded images
  Array.from(event.target.files!).forEach((image) =>
    dataTransfer.items.add(image)
  );

  const files = dataTransfer.files;
  const displayUrl = URL.createObjectURL(event.target.files![0]);

  return { files, displayUrl };
}