douyong1886 2019-05-07 11:30
浏览 119

使用angular 7到服务器提交图像文件和其他表单数据

I have form submitting details of a vehicle including VIN, driver and a photo of the vehicle. My problem is submitting the form data details together with the image to a server am using laravel as my backend

uploadImage (e) {     

for (let i = 0; i < e.target.files.length; i++)
       {
  this.selectedFile = <File>e.target.files[i];

       }

  }

onSubmit(){  //this method is called when user submits data

const fd = new FormData();
     fd.append('file', this.selectedFile, this.selectedFile.name);
         this.http.post('http://localhost:8000/api/buses',
         {fd, 'formdata':this.form}).subscribe((data)=>console.log(data))
}

//this is the code receiving the image
 if($request->has('file')) {

    // Get filename with the extension
    $filenameWithExt = $request->file('file')->getClientOriginalName();
    // Get just filename
    $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
    // Get just ext
    $extension = $request->file('file')->getClientOriginalExtension();
    // Filename to store
    $fileNameToStore = $filename . '_' . time() . '.' . $extension;
    // Upload Image
    $path = $request->file('file')->storeAs('public/cover_images', $fileNameToStore);

echo json_encode($fileNameToStore);

}

To submit the image and the other formdata to my backend server

  • 写回答

1条回答 默认 最新

  • douyi4544 2019-05-07 12:12
    关注

    You are posting the wrong information. Please change this as follows.

    fd.append('file', this.selectedFile, this.selectedFile.name);
             this.http.post('http://localhost:8000/api/buses',
             fd).subscribe((data)=>console.log(data))
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿