【问题标题】:Angular displaying REST Response after file upload文件上传后显示 REST 响应的角度
【发布时间】:2021-06-01 06:41:10
【问题描述】:

我有一个 Angular 应用程序,我想上传一个文件并根据上传的文件显示一个字符串。 我对一个 Rest API 进行了 post 调用,这个 API 返回一个响应,我想显示这个响应的内容。但是有些问题是因为我可以console.log 响应的内容,但我无法正常显示它(即作为标签或类似内容)。
谁能告诉我该怎么做?

这里是component.html

<div>
    <label class="image-upload-container btn btn-bwm">
        <span>Select Image</span>
        <input #imageInput type="file" accept="image/*" (change)="processFile(imageInput)">
    </label>

    <!--IT DOES NOT WORK  Displays [object Object] -->
    <input type=text name="prediction" [(ngModel)]="prediction" />

    <!--IT DOES NOT WORK  Displays [object Object] -->
    {{prediction}}
</div>

这里是component.ts

import { Component, OnInit } from '@angular/core';
import { ImageService } from '../../services/image.service';

class ImageSnippet {
  constructor(public src: string, public file: File) { }
}

@Component({
  selector: 'app-image',
  templateUrl: './image.component.html',
  styleUrls: ['./image.component.css']
})
export class ImageComponent implements OnInit {

  constructor(private imageService: ImageService) { }
  prediction!: String;
  ngOnInit(): void {
  }
  processFile(imageInput: any) {
    const file: File = imageInput.files[0];
    const reader = new FileReader();

    reader.addEventListener('load', (event: any) => {

      let selectedFile = new ImageSnippet(event.target.result, file);
      this.imageService.uploadImage(selectedFile.file).subscribe(
        (response) => {
          
          this.prediction = response.toString();// IT DOES NOT WORK  Displays [object Object] 
          // console.log() WORKS: it logs something like this: {prediction: "jeges, tensor([0.0530, 0.0999, 0.8471])"}
//which is the exact string that I want to display
          console.log(response)
        }        
    });
    reader.readAsDataURL(file);
  }
}

这里是service.ts

...
uploadImage(imageFile: File): Observable<Response> {
    const formData = new FormData();
    formData.append('imageFile', imageFile);
    return this.http_client.post<Response>(this.url, formData);
  }
...

这里是 API:

@app.route('/api/image-upload', methods=['POST'])
def call_predict():
    imageFile = request.files['imageFile']
    prediction = makePrediction(imageFile)
    resp = make_response({"prediction": prediction}, 200)
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp

【问题讨论】:

  • 你在response上有一个toString()调用,所以它会导致[object Object]显示,就像这样this.prediction = response
  • 无法编译。Failed to compile. src/app/components/image/image.component.ts:34:11 - error TS2740: Type 'Response' is missing the following properties from type 'String': charAt, charCodeAt, concat, indexOf, and 37 more. 34 this.prediction = response;
  • 等一下,让我编辑一下

标签: angular typescript response console.log


【解决方案1】:

您的response 上有一个toString() 呼叫,这导致string 显示。因此,让我们从正确修复您的文件处理程序函数开始:

因此,首先要像这样修复您的服务请求处理程序

this.imageService.uploadImage(selectedFile.file).subscribe(
  (response) => {
    this.prediction = response;
  }        
});

因此,此更改会导致您的 HTML 出现问题,因此我们需要对其进行调整,因为 this.prediction 不是 string

<div>
    <label class="image-upload-container btn btn-bwm">
        <span>Select Image</span>
        <input #imageInput type="file" accept="image/*" (change)="processFile(imageInput)">
    </label>

    <!-- here we'll bind according to the structure you gave in your code -->
    <input type=text name="prediction" [(ngModel)]="prediction.prediction"/>

    <!-- Since we have an object we cant just dump it here, so lets use a json pipe -->
    {{prediction | json}}
</div>

最后,您班级中的成员prediction 必须是Response

prediction!: Response;

所以我认为这应该可行,试试看...

【讨论】:

  • 有效的是:prediction!: Response; plus this.prediction = response; plus {{prediction | json}}
  • 哦,好吧,让我修复它...我认为这会更容易,这就是我更改它的原因...
  • 不管怎样,谢谢你的回答,对我帮助很大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 2021-10-20
  • 1970-01-01
  • 2018-06-04
  • 2017-02-21
  • 1970-01-01
相关资源
最近更新 更多