首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Graphql magento2的图像上传类型

Graphql magento2的图像上传类型
EN

Stack Overflow用户
提问于 2022-01-03 05:14:24
回答 1查看 382关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2022-01-05 02:31:17

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70561924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档