php - 如何在 Laravel 中检索 FormData

标签 php angular laravel-5

我正在将 Angular 2 的表单数据发送到 Laravel API,以保存从 RecordRTC js 录制的语音。检查控制台上的文件名、文件类型和 blob 文件。它正在显示。但无法在 Laravel 后端代码上检索。

 public uploadToServer() {
    let blob  = this.recordRTC instanceof Blob ? this.recordRTC : this.recordRTC.blob;
    let fileType = blob.type.split('/')[0] || 'audio';
    let fileName = (Math.random() * 1000).toString().replace('.', '');
    if (fileType === 'audio') {
      fileName += '.' + (!!navigator.mozGetUserMedia ? 'ogg' : 'wav');
    } else {
      fileName += '.webm';
    }
    // create FormData
    var formData: FormData = new FormData();
    console.log(fileName);
    console.log(blob);
    console.log(fileType);
    formData.append(fileType + '-filename', fileName);
    formData.append(fileType + '-blob', blob);
    console.log(formData);
    this.recordingService.saveRecording(formData).subscribe(
      data => this.saveRecordingSuccess(data),
      error => this.saveRecordingFail(error)
    );
  }

Laravel 代码:-

 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;
    }
}

在 laravel 代码中我没有收到任何文件和发布数据。

最佳答案

你必须在你的表单上放置 mime 类型,就像在 JQuery Ajax 中一样,具有属性

mimeType: "multipart/form-data"

关于php - 如何在 Laravel 中检索 FormData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50911043/

相关文章:

angular - FormArray 动态形式angular

redirect - laravel 5 Blade 如何在 Blade 模板中重定向

javascript - 如何读取两个对象的Json?

php - CodeIgniter:所有 $this->db->query() 方法调用的 SQL 审计?

php - Swift 邮件附件

angular - Typescript 组件选择器应命名为 undefined

PHP/MySQL 搜索以弥补遗漏的连字符

angular - Kendo Sortable for Angular - 获取新的索引位置

php - Laravel 5.5 导出 EXCEL

mysql - Eloquent - 多对多,用户主题按用户id删除所有主题