简体   繁体   English

如何在 fastAPI 中定义 REST 端点<input type="file"> (并使它与角度一起工作)?

[英]How to define a REST endpoint in fastAPI for <input type="file"> (and make it work with angular)?

I'm doing this angular tutorial .我正在做这个angular 教程 I'm trying to create a backend endpoint using python fastAPI that receives the data via HttpClient POST, but I'm struggling to do so.我正在尝试使用 python fastAPI 创建一个后端端点,该端点通过 HttpClient POST 接收数据,但我正在努力这样做。 (angular 13.0.2, python 3.7, fastapi 0.70.0) (角度 13.0.2,python 3.7,fastapi 0.70.0)

html template code: html模板代码:

<input type="file" class="file-input" (change)="onFileSelected($event)" #fileUpload> 

<div class="file-upload">

    {{fileName || "No file uploaded yet."}}

    <button mat-mini-fab color="primary" class="upload-btn" (click)="fileUpload.click()">
        <mat-icon>attach_file</mat-icon>
    </button>
</div>

corresponding component ts code:对应组件ts代码:

  onFileSelected(event) {

    const file: File = event.target.files[0]
    console.log(`onFileSelected(${file.name})`)

    if (file) {
      this.fileName = file.name;
      const formData = new FormData();
      formData.append("thumbnail", file);
      const upload$ = this.http.post("http://localhost:8000/thumbnail-upload", formData);
      upload$.subscribe();
      console.log("upload done?")
  }

fastapi code: inpired by this fastapi 代码:受此启发

@app.post("/thumbnail-upload")
def create_file(file: UploadFile = File(...)):
    print(file)
    return {"file_size": len(file)}

There seems to be some OK communication going on, but I'm stuck here:似乎正在进行一些正常的沟通,但我被困在这里:

output from uvicorn server: output 来自 uvicorn 服务器:

←[32mINFO←[0m:     127.0.0.1:18231 - "←[1mPOST /thumbnail-upload   HTTP/1.1←[0m" ←[31m422 Unprocessable Entity←[0m

output in browser debug console:浏览器调试控制台中的 output:

zone.js:2863 POST http://localhost:8000/thumbnail-upload 422 (Unprocessable Entity)
core.mjs:6495 ERROR HttpErrorResponse {headers: HttpHeaders, status: 422, statusText: 'Unprocessable Entity', url: 'http://localhost:8000/thumbnail-upload', ok: false, …}

How do I need to implement the fastAPI endpoint to get this running?我需要如何实现 fastAPI 端点才能使其运行? Disclaimer: I'm pretty new to angular/fastapi, if I forgot to include necessary information, pls.免责声明:我对 angular/fastapi 很陌生,如果我忘记包含必要的信息,请。 tell me what's missing;)告诉我缺少什么;)

The body of the 422 error message will give you exactly what field is missing. 422 错误消息的正文将准确地告诉您缺少的字段。 From your code it seems to indicate that you're submitting the file under the thumbnail name, but you're using the file name in your FastAPI route.从您的代码中,它似乎表明您正在以thumbnail名称提交文件,但您在 FastAPI 路由中使用了file名。 Since those two doesn't match, FastAPI doesn't find the file you're assuming is present.由于这两个不匹配,FastAPI 找不到您假设的文件存在。 Either rename the form parameter to file or rename the FastAPI argument to thumbnail :将表单参数重命名为file或将 FastAPI 参数重命名为thumbnail

@app.post("/thumbnail-upload")
def create_file(thumbnail: UploadFile = File(...)):
    print(thumbnail)
    return {"file_size": len(thumbnail.file.read())}

The UploadFile object also exposes file to interact with the spooled file behind the scenes. UploadFile object 还公开file以与幕后的假脱机文件进行交互。 Since it doesn't give you the bytes directly, I don't think calling len on it will do what you expected it to do.由于它不直接给你字节,我不认为在它上面调用len会做你期望它做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM