【问题标题】:Image upload type for Graphql magento2Graphql magento2 的图片上传类型
【发布时间】:2022-01-03 05:14:24
【问题描述】:

有没有办法使用graphql上传图片而不在magento2中使用base64格式

【问题讨论】:

    标签: file-upload graphql magento2


    【解决方案1】:
    public function uploadFile($fileData)
        {
            // convert base64 string to image and save as file on server.
            $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;
        }
    

    查看更多:https://magento.stackexchange.com/a/351629/101754

    【讨论】:

    • 是的,我目前正在使用这个解决方案,但如果有任何其他方法,不使用 base64
    猜你喜欢
    • 2020-10-20
    • 2019-04-30
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2010-11-27
    • 2014-05-10
    相关资源
    最近更新 更多