我正在try 根据一个curl 请求发出一个Angular 请求,这个请求是:

 curl -k -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/octet-stream" --data-binary '@/home/linux-user/Desktop/folder/cool-folder/src/assets/img/default-image.png' -X POST https://$ENDPOINT:6060/v1/user/photo\?id\=3591\&image\=996ab426-eb57-4f79-94f4-cfea9aef2cd9 |  gunzip | jq

*仅供参考:请求成功,但图像到达时已损坏,而且我正在通过API将图像发送到AWS

ImageService:

  private postHeaders(token: string): HttpHeaders {
      return new HttpHeaders({
        'Content-Type': 'application/octet-stream',
        Authorization: `Bearer ${token}`
      });
    }

uploadImage(file: File, token: string): Observable<any> {
      const headers = this.postHeaders(token);
  
      const formData = new FormData();
      formData.append('file', file);
  
      return this.http.post(this.endpoint, formData, { headers, responseType: 'text' })
        .pipe(
          map((response: any) => JSON.parse(response)),
          catchError((error: any) => throwError(error))
        );
    }

Component

onFileChange(event: any) {
    const file = event.target.files[0];
    console.log(file)
    if (file) {
      this.imageService.uploadImage(file, this.token)
        .subscribe((response) => {
          console.log(response); // Handle the response data here using 'jq' or other methods.
        }, (error) => {
          console.error(error);
        });
    }
  }


console.log(file)

Another example of the component above

tried binary

 onFileChange(event: any) {
    const file = event.target.files[0];
    console.log(file)
    if (file) {
      this.readFileContent(file);
    }
  }

  readFileContent(file: File) {
    const reader = new FileReader();

    reader.onload = (e: any) => {
      const binaryData = e.target.result; // This contains the binary data of the file

      this.imageService.uploadImage(new Blob([binaryData]), this.token)
        .subscribe((response) => {
          console.log(response); // Handle the response data here
        }, (error) => {
          console.error(error);
        });
    };

    reader.readAsArrayBuffer(file);
  }

Html

<input type="file" (change)="onFileChange($event)">

推荐答案

显然,根据那句话,简单地传递文件应该是有效的:http://www.sibenye.com/2017/02/17/handling-image-upload-in-a-multitiered-web-application-part-1/

必须用FormData才能发送multipart/form-data.

onFileChange(event: any) {
    const file = event.target.files[0];
    console.log(file)
    if (file) {
      this.readFileContent(file);
    }
    
    // part to send data
    let url = '/url';
    let headers = new Headers();
    headers.set('Content-Type', 'application/octet-stream');
    headers.set('Upload-Content-Type', file.type)
    // add authorization etc too
    let options = new RequestOptions({ headers: this.headers });
    this.http.post(url, file, options).subscribe()
  }

Angular相关问答推荐

Angular前端调用ALB身份验证后的后端并重定向

第15角Cookie的单元测试用例

CDK虚拟滚动由于组件具有注入属性而在滚动时看到错误的值

Angular 环境本地无文件替换

筛选器不会从文本输入触发更改事件

更改动态表单控制Angular 的值

如何在Primeng中实现自定义排序

如何在独立应用程序中全局禁用基于媒体查询的Angular 动画?

使用 Angular 类构造函数来获取依赖项

如何在 Angular RxJS 中替换订阅中的 subscrib?

Angular 路由中的模块构建失败

Angular,数据显示在控制台但不在应用程序中

http 调用的 RxJs 数组

没有调用订阅的原因是什么?

Angular模板错误:Property does not exist on type component

如何在 Angular 4 中使用 Material Design 图标?

等待多个promises完成

Angular 4中的md-select中的onselected事件

包 '@angular/cli' 不是依赖项

找不到@angular/common/http模块