cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading a base64 Image to Media folder via Javascript

Uploading a base64 Image to Media folder via Javascript

Any ideas how I may do that from the Admin panel. I've got the image in base64 but just trying to figure out how I can now upload it to the media folder.

 

Pointing me to code in magento that shows a rough example is totally an acceptable answer Smiley Wink

 

Thanks!

 

2 REPLIES

Re: Uploading a base64 Image to Media folder via Javascript

Hello @dylandadob,

 

There is no magento things required for that you can do with file_put_content

 

file_put_contents($output_file, file_get_contents($base64_string));

Here output file will be file  you created with fopen() , so create file with fopen and put your code with file_put_content

 

If you want to use file_put_content with magento way then

 

add dependency in your class for

 

    Magento\Framework\Filesystem\DriverInterface

 

and with $fileDriver object you can call filePutContents Method like below

 

$file->filePutContents($path, $content);

 

 If my answer solves your concern then hit kudos button and mark as accepted

 

 

 

 

Re: Uploading a base64 Image to Media folder via Javascript

Hi @dylandadob

 


Lets 's say you have base64_encoded image string in a variable called $imageString

 

$imageString = "your_base64_encoded_image";

 

First you need to decode by using base64_decode function and then you need to give full path along with filename in file_put_contents functions.

 

Below is the code for the same :

 

$imageString = "your_base64_encoded_image";
file_put_contents('../pub/media/MyFile.jpg', base64_decode($imageString));


If you just wanted to try first this - create a php file in root folder of magento 2

 

Then put above code in it - then run via browser and check

 

It will generate image file for the same

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution