将文件从Angular 7上载到Laravel时出现问题

将文件从Angular 7上载到Laravel时出现问题,angular,laravel,Angular,Laravel,我搜索了很多,但没有问题可以解决我的问题,我正在尝试在我的Laravel6.6版本中向服务器发送一个简单的图像 这是我的棱角7面 const formData = new FormData(); formData.append('file', this.files[0]); this.categoryService.store(category, formData).subscribe( response => { console.log(respon

我搜索了很多,但没有问题可以解决我的问题,我正在尝试在我的Laravel6.6版本中向服务器发送一个简单的图像

这是我的棱角7面

    const formData = new FormData();
    formData.append('file', this.files[0]);

    this.categoryService.store(category, formData).subscribe(
      response => { console.log(response); },
      error => {
        console.log(error);
      }
    );

  public store(categoryData: CategoryModel, filesData: FormData): Observable<any> {
    return this.http.post(this.mainConfig.config.default.mainUrl + this.STORE_CATEGORY_URL, filesData)
   .pipe(timeout(this.timeOut));
  }
我发现了我的问题,我的API的每个请求都有一个令牌拦截器,如下所示:

  const newRequest = request.clone({
        setHeaders: {
          Authorization: `Bearer ${this.token}`
        },
        body: {
          ...request.body,
          companyCode: this.userData.companyCode,
        },
      });
当我删除这行

   body: {
              ...request.body,
              companyCode: this.userData.companyCode,
            },

它工作正常,我使用它在后端发送租户数据库连接的公司代码,在每个请求中,我都发送公司代码,但是,当我在formData中使用它时,为什么不起作用?

上传应该始终使用
httpClient
上的
put
请求完成,因为文件上传将在较小的块(4kb)中完成
post
无法连续传输,仅在传输第一个数据块后才会进行剪切。您可以直接使用
文件
作为主体。您不应该对图像使用
FormData
,因为它会附加
FormData
标题并损坏图像

this.categoryService.store(category,this.selectedFile).subscribe(
      response => { console.log(response); },
      error => {
        console.log(error);
      }
    )
按如下方式修改服务

 public store(categoryData: CategoryModel,selectedFile:any): Observable<any> {
    return this.http.put(this.mainConfig.config.default.mainUrl + this.STORE_CATEGORY_URL, selectedFile)
   .pipe(timeout(this.timeOut));
  }
公共存储(categoryData:CategoryModel,selectedFile:any):可观察{
返回此.http.put(this.mainConfig.config.default.mainUrl+this.STORE\u CATEGORY\u URL,selectedFile)
.pipe(超时(this.timeout));
}

上传应始终使用
httpClient
上的
put
请求完成,因为文件上传将在较小的块(4kb)中完成
post
无法连续传输,仅在传输第一个数据块后才会进行剪切。您可以直接使用
文件
作为主体。您不应该对图像使用
FormData
,因为它会附加
FormData
标题并损坏图像

this.categoryService.store(category,this.selectedFile).subscribe(
      response => { console.log(response); },
      error => {
        console.log(error);
      }
    )
按如下方式修改服务

 public store(categoryData: CategoryModel,selectedFile:any): Observable<any> {
    return this.http.put(this.mainConfig.config.default.mainUrl + this.STORE_CATEGORY_URL, selectedFile)
   .pipe(timeout(this.timeOut));
  }
公共存储(categoryData:CategoryModel,selectedFile:any):可观察{
返回此.http.put(this.mainConfig.config.default.mainUrl+this.STORE\u CATEGORY\u URL,selectedFile)
.pipe(超时(this.timeout));
}

我解决了检查FormData实例的问题,现在它正在工作

if (!!this.token) {
          let newRequest;
          if (request.body instanceof FormData) {
            const formData = request.body;
            formData.append('companyCode', this.userData.companyCode);
            newRequest = request.clone({
              setHeaders: {
                Authorization: `Bearer ${this.token}`
              },
            });
            return next.handle(newRequest);
          } else {
            newRequest = request.clone({
              setHeaders: {
                Authorization: `Bearer ${this.token}`
              },
              body: {
                ...request.body,
                companyCode: this.userData.companyCode,
              },
            });
            return next.handle(newRequest);
          }
        }

我解决了检查FormData实例的问题,现在它正在工作

if (!!this.token) {
          let newRequest;
          if (request.body instanceof FormData) {
            const formData = request.body;
            formData.append('companyCode', this.userData.companyCode);
            newRequest = request.clone({
              setHeaders: {
                Authorization: `Bearer ${this.token}`
              },
            });
            return next.handle(newRequest);
          } else {
            newRequest = request.clone({
              setHeaders: {
                Authorization: `Bearer ${this.token}`
              },
              body: {
                ...request.body,
                companyCode: this.userData.companyCode,
              },
            });
            return next.handle(newRequest);
          }
        }

试着像这样获取文件:
$file=$request->file('image')
当我尝试使用postman时,我的后端simple工作正常,但当我尝试使用angular时,angular不工作=/我发现了我的问题,我将编辑我的问题,请尝试按如下方式获取您的文件:
$file=$request->file('image')当我尝试与邮递员我的后端简单的工作,但当我去尝试与角度不工作=/我发现我的问题,我会编辑我的问题plz看