Include an image in a Material-UI card without specifying a file path

I'm looking to include an image in a CardMedia component, but the image is currently stored as a base64 string like this:

iVBORw0KGgoAAAANSUhEUgAAASwAAACoCAMAAABg99fBOxrQANHibkS1KoplYAFHgUGz85g6Ggnn2ysaMXSlccHislKRm7uI10eB9piJYuPjE3epRveUuwrTNidn9ad2ZMZFxNfoOJjTsOGESjp...

After converting the string to a blob/file, I am unsure of how to attach the image to the CardMedia component since it appears to only accept a path to an image.

https://i.sstatic.net/lo8Pm.png

Answer №1

Indicate that the data is encoded in base64. For example, I have used SVG; just make sure to update your MIME type accordingly (e.g., image/png)

const { Card, CardMedia } = MaterialUI;

ReactDOM.render(
  <Card>
    <CardMedia
      style={{ height: 100, width: 100 }}
      image="data:image/svg+xml;base64,PHN2ZyBjbGFzcz0iTXVpU3ZnSWNvbi1yb290IiBmb2N1c2FibGU9ImZhbHNlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgYXJpYS1oaWRkZW49InRydWUiID48cGF0aCBkPSJNMTUgM2wyLjMgMi4zLTIuODkgMi44NyAxLjQyIDEuNDJMMTguNyA2LjcgMjEgOVYzaC02ek0zIDlsMi4zLTIuMyAyLjg3IDIuODkgMS40Mi0xLjQyTDYuNyA1LjMgOSAzSDN2NnptNiAxMmwtMi4zLTIuMyAyLjg5LTIuODctMS40Mi0xLjQyTDUuMyAxNy4zIDMgMTV2Nmg2em0xMi02bC0yLjMgMi4zLTIuODctMi44OS0xLjQyIDEuNDIgMi44OSAyLjg3TDE1IDIxaDZ2LTZ6Ij48L3BhdGg+PC9zdmc+"
    />
    <CardMedia
      component={() => (
        <img
          alt="test"
          src="data:image/svg+xml;base64,PHN2ZyBjbGFzcz0iTXVpU3ZnSWNvbi1yb290IiBmb2N1c2FibGU9ImZhbHNlIiB4...
      )}
    />
  </Card>, 
  document.getElementById("root") 
);
<body>
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@material-ui/core@latest/umd/material-ui.development.js"></script>

  <div id="root"></div>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"MongoDB encountered a circular structure while attempting to convert it to JSON

Upon running the command from the console, I receive an ordinal object as follows: >> db.query.find({"user_id":"111"}) { "_id" : ObjectId("58bbf5bf17cc9100046bdff"), "query" : "my query", "user_id" : "111", "links" : [ ] } However, when executing t ...

Navigating a roster of users in JavaScript

I'm dealing with a collection of user data stored in an array accountsade: [ { id: 0.4387810413935975, name: "Adrian", password: "345", userName: "nathanael" }, { id: 0. ...

AngularJS combined with jVectorMap allows for the seamless rendering of small interactive

I am facing a similar issue to one discussed here, but with some minor differences. Initially, my map loaded without any problem on the site. However, after adding Angularjs to the site, I noticed a 'small' map issue. It seems like a simple code ...

Having trouble retrieving state parameters when trying to launch a modal in AngularJS

When the "view detail" link is clicked, I wanted to open a modal for a detailed view of a specific user. However, I encountered an issue where I couldn't retrieve the user ID in the resolve attribute of $uibModal.open(). Strangely enough, the ID is av ...

Best practices for effectively managing a sizable dataset in Vue.js

My task at hand is to create a visualization dashboard that can operate solely in the browser, utilizing Vue.js, D3, and JavaScript. The dataset I am working with is stored in a .json file that is 48 MB in size. However, upon parsing the file using d3.json ...

Issue arises when incorporating accordion toggle with navtabs (Bootstrap)

Currently, I am working on a navigation layout using Bootstrap that will be displayed as tabs on desktop and as an accordion on mobile devices. The setup is almost complete, but there is an issue that I'm struggling to resolve. The problem arises whe ...

Tips for implementing import or require in Electron

Currently, I am in the process of developing an electron app. I would like to incorporate some functions from other JS files into my project. However, when attempting to use the import statement, an error is triggered "cannot use import statement outside a ...

Error alert: Unable to find the specified word

Having trouble writing a word to the database due to an error: ReferenceError: Patikrinta is not defined. Below is my ajax script for sending data to a PHP file, and then the PHP script itself if needed. Haven't found a solution on Stack Overflow. $s ...

Retrieve the bounding rectangle of a div that has the CSS style `display: contents` using the getBoundingClientRect

My objective is to apply styling and obtain the bounding box of an entire "row" within a CSS grid, including features like highlighting when hovering over it. To achieve the styling aspect, I make use of the display: contents property, so that the styles ...

"Encountering an error when trying to access undefined property in templates

The content displayed in my component template is not what I expected when using @Output to pass an object from a parent container. While attempting to bind {{selectedMovDetail|json}} in the template, the output shows as { "name": "The Walking Dead","rati ...

Executing JavaScript functions when a browser tab is closed

When a user closes the browser tab, I want to call a specific JavaScript function. However, I only want this to occur when the user is actually closing the browser, not during page refreshes, link navigation, form submissions, or pressing the back button. ...

Troubleshooting problem with executing JavaScript through a PHP script

I am currently working on a php script that edits records in a MySQL table. However, I am encountering an issue with refreshing the page using javascript while passing the record number. Below are a few lines from my php script: if ($mode == "edit") { $i ...

What is the best way to eliminate "?" from the URL while transferring data through the link component in next.js?

One method I am utilizing to pass data through link components looks like this: <div> {data.map((myData) => ( <h2> <Link href={{ pathname: `/${myData.title}`, query: { ...

I want to use the enter key for posting, but also have the ability to use the shift and enter key to create a

$("#post_text").keydown(function(e){ // Checking if Enter key was pressed without shift key if (e.keyCode == 13) { // Prevent default behavior e.preventDefault(); } if (e.keyCode == 13 && !e.shiftKey) { alert('test'); } }); I a ...

Error encountered in react-test-renderer test due to TypeError: Attempting to access properties of an undefined variable, specifically trying to read the 'current

Having trouble with my unit tests using react-test-renderer. Even though the component I'm testing is working fine, the test won't pass. Can someone please help me figure out what I'm doing wrong? Below is the code for the component: // ...

Calculate the sum of multiple user-selected items in an array to display the total (using Angular)

Within my project, specifically in summary.component.ts, I have two arrays that are interdependent: state: State[] city: City[] selection: number[] = number The state.ts class looks like this: id: number name: string And the city.ts class is defined as f ...

The presence of a WebXR overlay disrupts the functionality of the ThreeJS AR Controller

Currently, I am working on a school project which involves creating a webshop where users can view products in Augmented Reality (AR). I have used WebXR to add an overlay to the AR session that includes a button for repositioning the model. However, I noti ...

Transform a list of file directories into a JSON format using ReactJS

I am developing a function that can convert a list of paths into an object structure as illustrated below; this script will generate the desired output. Expected Output data = [ { "type": "folder", "name": "dir1& ...

Click on the checkbox to activate it using JavaScript

I'm trying to use JavaScript to toggle the checkbox status when clicking a button. The first button is supposed to uncheck the box and it's working correctly: function clear() { document.getElementById("check").checked = ""; } However, I al ...

What is the best way to add custom styles to an Ext JS 'tabpanel' xtype using the 'style

Is there a way to change the style of a Ext.tab.Panel element using inline CSS structure like how it's done for a xtype: button element? { xtype: "button", itemId: "imageUploadButton1", text: "Uploader", style: { background : ' ...