我假设您正在以某种关系保存该文件。
例如:个人资料图片等。(因此在这种情况下,您尝试将该文件附加到用户)
以这个为例。
在user model 中你可以定义你的关系
public $attachOne = [
'avatar' => 'System\Models\File'
];
现在当收到来自带有base64 编码文件的移动应用程序的请求时
您提到您已成功转换为 jpeg,但例如我也为此添加了粗略的代码。
// we assume you post `base64` string in `img`
$img = post('img');
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$imageData = base64_decode($img);
// we got raw data of file now we can convert this row data to file in dist and add that to `File` model
$file = (new \System\Models\File)->fromData($imageData, 'your_preferred_name.jpeg');
// attach that $file to Model
$yourModel->avatar = $file;
$yourModel->save();
或如果您不使用关系保存该文件,您现在可以使用$file->id 指向该文件,下次查找或保存以供以后使用。
// next time
// $file->id
$yourFile = \System\Models\File::find($file->id);
现在您的文件已保存,下次您需要该文件时可以直接使用该文件
$imageData = $yourModel->avatar->getContents();
$imageBase64Data = base64_encode($imageData);
// $imageBase64Data <- send to mobile if needed.
如果有什么不清楚的地方请评论。