Tsung-Jung Tsai (TJ_Tsai)
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    UploadFile === ###### tags: `Python / FastAPI` ###### tags: `Python`, `PyPi`, `backend`, `fastapi`, `UploadFile`, `表單`, `form` <br> [TOC] <br> ## 討論 ### FastAPI 檔案上傳錯誤說明與修正 - ### Question 關於 Python FastAPI ```python @router.post('/upload') async def upload_file(file): print('upload: ', file) ``` ``` $ curl -X POST http://localhost:8080/client/upload -F "file=@20250401-packages.zip" {"detail":[{"type":"missing","loc":["query","file"],"msg":"Field required","input":null}]} ``` 哪裡出問題? - ### gpt-4o (2025/04/16) ### ❌ 錯誤訊息 當使用以下 FastAPI 程式碼: ```python @router.post('/upload') async def upload_file(file): print('upload: ', file) ``` 搭配這個 `curl` 指令: ```bash curl -X POST http://localhost:8080/client/upload -F "file=@20250401-packages.zip" ``` 會出現以下錯誤: ``` {"detail":[{"type":"missing","loc":["query","file"],"msg":"Field required","input":null}]} ``` ### 📌 問題說明 FastAPI 預設會將參數 `file` 解讀為 **Query Parameter**,但你實際上是透過 `multipart/form-data` 上傳檔案。 若沒有使用 `UploadFile` 和 `File(...)` 來明確宣告這是檔案,FastAPI 就不會從表單中解析檔案上傳內容。 --- ### ✅ 正確做法:使用 `UploadFile` + `File(...)` 請修改你的 API 為: ```python from fastapi import APIRouter, UploadFile, File router = APIRouter() @router.post('/upload') async def upload_file(file: UploadFile = File(...)): print('upload:', file.filename) return {"filename": file.filename} ``` 這樣 FastAPI 才會從表單欄位中解析上傳的檔案。 --- ### ✅ 附加需求(可選) 如果你還要額外上傳欄位,例如 `token`: ```python from fastapi import Form @router.post('/upload') async def upload_file( file: UploadFile = File(...), token: str = Form(...) ): ... ``` 對應的 `curl`: ``` curl -X POST http://localhost:8080/client/upload \ -F "file=@20250401-packages.zip" \ -F "token=abc123" ``` --- ### 🔧 延伸功能建議 你可以進一步擴充功能,如下: - ✅ 檔案上傳解壓縮 - ✅ 上傳後自動觸發後端流程 - ✅ Token 自動產生與驗證 - ✅ 支援多用戶、多組 Token 與權限 - ✅ Swagger UI 支援測試 - ✅ 解壓縮後回傳檔案清單 如需,我可以幫你模組化實作完整專案架構。 <br> ### RuntimeError: Form data requires "python-multipart" to be installed. - ### Question RuntimeError: Form data requires "python-multipart" to be installed. You can install "python-multipart" with: pip install python-multipart - ### gpt-4o (2025/04/16) > FastAPI 檔案上傳錯誤與解法 ### 錯誤訊息 當你在 FastAPI 中使用 `UploadFile` 或 `Form` 接收 multipart/form-data 時,若出現以下錯誤: ``` RuntimeError: Form data requires "python-multipart" to be installed. You can install "python-multipart" with: pip install python-multipart ``` 表示你尚未安裝處理表單上傳所需的依賴套件。 --- ### ✅ 解法 請安裝 `python-multipart`: ``` pip install python-multipart ``` 這個套件會讓 FastAPI 能解析上傳表單,特別是 `UploadFile` 和 `Form(...)` 所需的 `multipart/form-data` 格式。 --- ### 📦 建議 dependencies 若你正在開發 FastAPI 專案,建議將以下內容加入 `requirements.txt` 或安裝: ``` fastapi uvicorn python-multipart ``` 你也可以一鍵安裝全部推薦套件: ``` pip install "fastapi[all]" ``` 這樣會連 `python-multipart`、`jinja2`、`pydantic-settings` 等都一併裝好。 --- ### ✅ 正確的上傳路由範例 ```python= from fastapi import APIRouter, UploadFile, File router = APIRouter() @router.post('/upload') async def upload_file(file: UploadFile = File(...)): print('upload:', file.filename) return {"filename": file.filename} ``` --- ### ✅ 對應的 curl 測試指令 ``` curl -X POST http://localhost:8080/client/upload -F "file=@20250401-packages.zip" ``` --- 有了這些設定後,FastAPI 就能正確處理 `multipart/form-data` 檔案上傳。 --- ### 🔧 延伸功能(可選): 你也可以加入以下功能來擴充你的 API: - ✅ Token 驗證 - ✅ 上傳後自動解壓縮 ZIP - ✅ 回傳檔案列表 - ✅ Swagger UI 自動化說明文件 - ✅ 多使用者權限控管與 Token 過期機制 如需範例,我可以提供分模組結構的專案範本。 <br> ### 📦 FastAPI 中的 `File(...)` 與 `...` 的意思 - ### gpt-4o (2025/04/16) ### 🔍 `File(...)` 是什麼? 在 FastAPI 中,我們用 `File(...)` 來處理從 `multipart/form-data` 上傳的檔案。 範例: ```python from fastapi import File, UploadFile async def upload_file(file: UploadFile = File(...)): ... ``` 這樣的寫法會讓 FastAPI 知道: - `file` 是從上傳表單來的檔案。 - 使用者 **必須提供這個檔案**,否則會報錯。 --- ### ✅ `...` 是什麼? `...` 是 Python 的內建物件,名稱是 `Ellipsis`,在 FastAPI / Pydantic 中有特別用途: - `...` 表示「這個欄位是 **必填** 的」。 - 等同於告訴 FastAPI:「這個欄位沒有預設值,也不能省略」。 範例比較: ``` # 必填 file: UploadFile = File(...) # 選填(預設為 None) file: UploadFile = File(None) ``` --- ### 📥 FastAPI 接收不同資料來源對照表 | FastAPI 類型 | 資料來源 | 寫法範例 | 說明 | |--------------|----------------------|------------------------|------------------------------| | `Query(...)` | URL 查詢字串 | `?id=123` | 來自網址後方的參數 | | `Form(...)` | 表單欄位(非檔案) | `application/x-www-form-urlencoded` 或 `multipart/form-data` | 接收一般表單欄位 | | `File(...)` | 上傳檔案(表單檔案) | `multipart/form-data` | 專門處理檔案上傳 | | `Body(...)` | JSON 請求主體 | `application/json` | 接收 JSON 結構資料 | --- ### 🚨 常見錯誤 如果你這樣寫: ```python async def upload_file(file): ... ``` FastAPI 會預設你是從 `query` 接收 `file`,而不是從表單上傳,導致出現錯誤訊息: ```json {"detail":[{"type":"missing","loc":["query","file"],"msg":"Field required","input":null}]} ``` --- ### ✅ 解法總結 - 要處理上傳檔案:使用 `file: UploadFile = File(...)` - `...` 表示這是 **必填欄位** - 要記得安裝必要套件: ```bash pip install python-multipart ``` --- 是否也需要我幫你補上 `Form(...)` 的對應範例一起整理? <br> {%hackmd vaaMgNRPS4KGJDSFG0ZE0w %}

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully