Fastapi 上传图片

Ner*_*ero 5 html python cordova fastapi

我尝试从 Cordova Web 应用程序将图像上传到 fastapi 并收到以下错误:

{"detail":[{"loc":["body","terms"],"msg":"field required","type":"value_error.missing"},{"loc":["body","privacy"],"msg":"field required","type":"value_error.missing"}]}

INFO:     192.168.1.129:51915 - "POST /upload/ HTTP/1.1" 422 Unprocessable Entity
Run Code Online (Sandbox Code Playgroud)

我的 FastApi 代码是:

@app.post("/upload/", dependencies=[Depends(valid_content_length)])
async def create_upload_file(file: bytes = File(...), terms: str = Form(...), privacy: str = Form(...)):
    allowedFiles = {"image/jpeg", "image/png", "image/gif", "image/tiff", "image/bmp", "video/webm"}
    if file.content_type in allowedFiles:
        filename = str(uuid.uuid4())
        with open("uploaded_images" + filename + file.filename, "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)
        return {"filename": file.filename}
    else:
        return "miau"
Run Code Online (Sandbox Code Playgroud)

客户端代码:

<form method="post" action="http://192.168.1.129:8080/upload">

                <div class="form_row">
                    <label for="myfile">Choose your image:</label>
                    <input type="file" id="myfile" name="file">
                </div>
                <div class="form_row">

                        <label>Accept:</label>
                        <label class="label-checkbox item-content">
                            <input type="checkbox" name="my-checkbox" value="privacy" required>
                            <div class="item-media">
                                <i class="icon icon-form-checkbox"></i>
                            </div>
                            <div class="item-inner">
                                <div class="item-title"><a src="" target="_blank">Privacy Policy</a></div>
                            </div>
                        </label>

                        <label class="label-checkbox item-content">
                            <input type="checkbox" name="my-checkbox" value="terms" required>
                            <div class="item-media">
                                <i class="icon icon-form-checkbox"></i>
                            </div>
                            <div class="item-inner">
                                <div class="item-title">Terms of Use</div>
                            </div>
                        </label>
                        <label class="label-checkbox item-content">
                            <input type="submit" name="submit" class="form_submit" id="submit" value="Send"/>
                        </label>
                </div>
            </form>
Run Code Online (Sandbox Code Playgroud)

如何解决问题?根据错误,主体是空的,但我不知道问题是什么。

谢谢 :)

lsa*_*abi 5

该错误几乎说明了一切

"loc":["body","terms"],"msg":"field required"
Run Code Online (Sandbox Code Playgroud)

"loc":["body","privacy"],"msg":"field required"
Run Code Online (Sandbox Code Playgroud)

这意味着您的表单未提交termsprivacy字段。

如果您查看HTML代码,您可以看到两个输入都有 name my-checkbox,而fastapi路线需要两个参数:privacyterms

更改输入的名称以匹配两个参数