0

I want to upload images using graphql using file not base64, can anyone tell me how can I implement this?

Thanks in advance!!

1

2 Answers 2

0

First convert your image into base64 then use that base64 string in to graphql input.

<?php

namespace Vendor\Module\Model\Upload;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\File;

class UploadImage implements ResolverInterface
{
    public function __construct(
        Filesystem $fileSystem,
        File $fileDriver
    ) {
        $this->fileSystem = $fileSystem;
        $this->fileDriver = $fileDriver;
    }
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $this->uploadFile($fileData);
    }

    public function uploadFile($fileData)
    {
        $uploadedFileName = "";
        $fileName = '';
        if (isset($fileData['name'])) {
            $fileName = $fileData['name'];
        } else {
            $fileName = rand() . time();
        }
        if (isset($fileData['filecontent'])) {
            $mediaPath = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
            $originalPath = 'ModuleName/Attachments/';
            $mediaFullPath = $mediaPath . $originalPath;
            if (!file_exists($mediaFullPath)) {
                mkdir($mediaFullPath, 0775, true);
            }
            /* Check File is exist or not */
            $fullFilepath = $mediaFullPath . $fileName;
            if ($this->fileDriver->isExists($fullFilepath)) {
                $fileName = rand() . time() . $fileName;
            }
            $fileContent = base64_decode($fileData['filecontent']);
            $savedFile = fopen($mediaFullPath . $fileName, "wb");
            fwrite($savedFile, $fileContent);
            fclose($savedFile);
            $uploadedFileName = "/" . $fileName ;
        }
        return $uploadedFileName;
    }
}

And the VAR is :

$fileData is array 
$fileData['filecontent'] = base64 string of file.
$fileData['name'] = file name.
4
0

Using base64 is the best solution.

Other solution: You can also get file data using file_get_contents() PHP function. And store in new file using file_put_contents() PHP function. This function creates an issue when data is large. When we use in graphql query then it returns only file data as a response.

Reference link :

https://www.w3schools.com/php/func_filesystem_file_get_contents.asp

https://www.w3schools.com/php/func_filesystem_file_put_contents.asp

https://www.php.net/manual/en/function.file-put-contents.php

https://www.faqcode4u.com/faq/411274/php-image-file-upload-and-convert-to-base64-without-saving-image

I suggest going with base64 implementation

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.