lundi 18 juin 2018

How to retrieve FormData in Laravel

I am sending formdata from Angular 2 to Laravel API to save the recorded voice from RecordRTC js. Checked the filename, filetype and blob file on console. it is showing. but not able to retrieve on Laravel backend code.

  // create FormData
    var formData: FormData = new FormData();
    formData.append(fileType + '-filename', fileName);
    formData.append(fileType + '-blob', blob);
    // http post.

Laravel Code:-

 public function saveRecording(Request $request)
  {
    $fileName = '';
    $tempName = '';
    $file_idx = '';

    if (!empty($_FILES['audio-blob'])) {
        $file_idx = 'audio-blob';
        $fileName = $_POST['audio-filename'];
        $tempName = $_FILES[$file_idx]['tmp_name'];
    }
    if (empty($fileName) || empty($tempName)) {
        if(empty($tempName)) {
            echo 'Invalid temp_name: '.$tempName;
            return;
        }
        echo 'Invalid file name: '.$fileName;
        return;
    }
    $filePath = public_path('voiceRecording/' . $fileName);

    // make sure that one can upload only allowed audio/video files
    $allowed = array(
        'webm',
        'wav'
    );
    $extension = pathinfo($filePath, PATHINFO_EXTENSION);
    if (!$extension || empty($extension) || !in_array($extension, $allowed)) {
        echo 'Invalid file extension: '.$extension;
        return;
    }
    if (!move_uploaded_file($tempName, $filePath)) {
      // error code
        return;
    }
}

In laravel code I have not receiving any files and post data.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire