从LUMEN用FormData在Angular中进行POST时请求为空

时间:2018-09-20 09:11:16

标签: angular laravel file-upload multipartform-data lumen

我正在尝试将文件上传到LUMEN后端。

发送到后端的标头是:

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Authorization: Bearer 
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 1034
Content-Type: multipart/form-data
Host: localhost:8000
Origin: http://evil.com/
Pragma: no-cache
Referer: http://localhost:4200/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36

有效负载是一个formData,其中附加了一个文件:

------WebKitFormBoundaryBh1H2ML8XFieaS2c
Content-Disposition: form-data; name="file[]"; filename="exportSales.csv"
Content-Type: text/csv


------WebKitFormBoundaryBh1H2ML8XFieaS2c--

上传文件的前端方法是:

store(file): Observable<{}> {

  let formData = new FormData();
  formData.append('file', file);

  return this.http.post(this.settings.api.uploadFile, formData, {headers: { 'Content-Type': 'multipart/form-data'} })
                   .catch((error:any) => Observable.throw(error || 'Server error'));
}

但是请求始终为空:

$router->group(['prefix' => 'v1'], function () use ($router) {
  $router->post('/upload', function (Request $request) use ($router) {
    dd($request->all());
  });
});

1 个答案:

答案 0 :(得分:0)

尝试在

中使用file.file
let formData = new FormData();
formData.append('file', file.file);

或向我发送console.log(file);