gpt4 book ai didi

angular - 如何使用 POST 方法在 Angular 中发送表单数据?

转载 作者:行者123 更新时间:2023-12-05 02:13:45 32 4
gpt4 key购买 nike

我有后端 API,它接受带有图像表单数据的 POST 方法,就像这样, enter image description here

像上面那样使用 Postman 时,一切正常。但是当我想在 Angular 中执行此操作时,它不起作用。

<!-- html template file -->
<input type="file" (change)="handleInputEvent($event)"/>
import {Component, OnInit} from '@angular/core';
import {MyDearFishService} from '../../my-dear-fish.service';

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

constructor(public service: MyDearFishService) {
}

ngOnInit() {
}

arrayOne(n: number): any[] {
return Array(n);
}

handleInputEvent($event) {

const image = $event.target.files[0];
this.service.recognizeFish(image);
}

}
// My service file (using HttpClient):
const rootUrl = 'https://...../api';

public recognizeFish(image: File): Promise<any> {
return new Promise((resolve, reject) => {

const formData = new FormData();
formData.append('image', image);

this.post('/image/identification', formData)
.toPromise()
.then(res => {
if (res['code'] === 0) {
console.log('=====================================');
console.log('Recognition failed, cause = ', res);
console.log('=====================================');
} else {
console.log('=====================================');
console.log('Recognition succeeded, res = ', res);
console.log('=====================================');
}
resolve();
})
.catch(cause => {
console.log('=====================================');
console.log('Recognition failed, cause = ', cause);
console.log('=====================================');
reject();
});
;
});
}

private getOptions(headers?: HttpHeaders, params?): HttpHeaders {
if (!headers) {
headers = new HttpHeaders().append('Content-Type', 'application/x-www-form-urlencoded');
}
return headers;
}

post(route: string, body: any, headers?: HttpHeaders): Observable<any> {
headers = this.getOptions(headers);
return this.http.post(rootUrl + route, body, {headers});
}

后端开发人员(使用 Flask 开发后端的人)给我这段代码:

@main.route("/image/identification", methods=['POST'])
@login_required
def identification():
image_file = request.files.get('image', default=None)
if image_file:
picture_fn = save_picture(image_file, 2)
return identif(picture_fn)
else:
return jsonify({'code':0, 'message':'image file error!'})

而且他还告诉我,当响应中的“code”属性为0时,表示错误,为1时,表示没有错误。当我在浏览器中测试我的 Angular 应用程序时,我遇到了这个错误: enter image description here

最佳答案

当我使用 angular 上传一些图片时,我会这样做:

public uploadImage (img: File): Observable<any> {
const form = new FormData;

form.append('image', img);

return this.http.post(`${URL_API}/api/imagem/upload`, form);

}

而且效果很好。所以,我认为你的代码中的问题是你没有将 formData 传递给你的 post 方法:

this.post('/image/identification', {files: {image: image}})
.toPromise()....

关于angular - 如何使用 POST 方法在 Angular 中发送表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693529/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com