2017-12-27 19 views
0

10 월을 사용하여 모바일 앱과 통신하는 API를 만듭니다. 이 프로세스에서는이 base64 이미지를 JPG 형식으로 변환하기 때문에이 부분이 문제가되지 않을 때까지 base64 형식의 이미지를 보내고 있습니다.이 JPG 이미지를이 JPG 이미지를 저장하는 두 번째 부분에 저장하고 있습니다. 10 월의 표준 프로세스. System_Files 파일을 저장하십시오.10 월 CMS - 업로드 파일이 포함 된 API

이 업로드를 수행하는 가장 좋은 방법은 무엇입니까?

답변

1

나는 그 파일을 어떤 관계로 저장한다고 가정한다.

for ex: profile picture etc.. (so in this case you try to attach that file to user)

이렇게 예를 들자면.

you mentioned you are successfully converting to jpeg but for example I just added rough code for that as well.

// 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(); 

OR if you don't save that file with relation you can now point that file with $file->id and find it next time or save it for later use.

// next time 
// $file->id 
$yourFile = \System\Models\File::find($file->id); 

이제 파일입니다 base64 인코딩 된 파일을 모바일 앱에서 요청을 수신 할 때 user model 내부

당신은

public $attachOne = [ 
    'avatar' => 'System\Models\File' 
]; 

지금의 관계를 정의 할 수 있습니다 다음 번에 해당 파일이 필요하면 직접 파일을 사용할 수 있습니다.

$imageData = $yourModel->avatar->getContents(); 
$imageBase64Data = base64_encode($imageData); 

// $imageBase64Data <- send to mobile if needed. 

불명확 한 점이 있으면 의견을 말하십시오.

+1

지원해 주셔서 감사합니다. – Crazy

+0

나는 이것이 오류입니다 : 비 객체의 속성을 얻으려고하면이 부분에 있다고 생각합니다 : // 해당 $ 파일을 Model $ yourModel-> avatar = $ file; – Crazy

+0

파일 객체가 생성되지 않은 것처럼 보이고, 거기에'$ imageData'를 가지고 좀 더 디버그 할 수 있습니다. –