python - 如何从 Angular 向 Flask 服务器发出 POST 请求
问题描述
我正在开发一个应用程序,该应用程序需要我从 Angular 前端将 csv 文件上传到 FLASK 服务器。我很难做到这一点。我如何将角度前端连接到后端烧瓶服务器。
这是我的 component.html
<div [hidden] = "submitted">
<form (ngSubmite) = "onSubmit()" action = "http://localhost:5000" method = "post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type = "submit" value="Upload">
</form>
这是我的 component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-upload',
templateUrl: './upload.component.html',
styleUrls: ['./upload.component.css']
})
export class UploadComponent implements OnInit {
submitted = false;
constructor() { }
onSubmit(){
this.submitted = true;
}
ngOnInit() {
}
}
这是我的烧瓶服务器
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/upload' , method = ['GET' ,'POST'])
def upload_File():
if request.method == 'POST':
#check if the psot request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
#if the user does not select file , browser alsos
#submite an empty part without filename
if file.filename == ' ':
flash('No selected file')
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#return render_template('success.html', name = filename)
return redirect(request.url)
return 'File Uploaded'
解决方案
- 导入
HttpClientModule
你AppModule
的imports
. - 像这样注入
HttpClient
您的组件(稍后将该逻辑移动到服务中)constructor(private readonly http: HttpClient) {}
- 最后在您的提交方法中,您可以执行 http 请求
this.http.post(url, body).subscribe()
推荐阅读
- javascript - GWT:将处理程序添加到本机 js 事件 onafterprint
- java - 唯一标识JAVA中终止的线程
- c# - unity register 泛型类型
- java - 辅助注入根据运行时检查注入不同的依赖项
- c# - 登录/注册时抛出 Xero API 错误 500
- module - SuiteCRM:仅用于自定义可导入模块的下拉菜单
- java - 如何只更改字符串中的字母,留下空格、连字符和撇号?
- sql-server - 如何检查primary不是同一个表中的parentID
- javascript - 如何按值查找多个数组元素?
- java - 在 com.esri.arcgisruntime maven 依赖项中找不到源附件