Yêu cầu python tải lên tệp nhị phân

Nhưng hãy nhớ rằng khi bạn nhập

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
4,
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
5,
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2 và các hàm khác từ
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
8, đó thực sự là các hàm trả về các lớp đặc biệt

Mẹo

Để khai báo Nội dung tệp, bạn cần sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2, vì nếu không, các tham số sẽ được hiểu là tham số truy vấn hoặc tham số nội dung [JSON]

Các tệp sẽ được tải lên dưới dạng "dữ liệu biểu mẫu"

Nếu bạn khai báo loại tham số chức năng vận hành đường dẫn của mình là

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9, FastAPI sẽ đọc tệp cho bạn và bạn sẽ nhận được nội dung là
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9

Hãy nhớ rằng điều này có nghĩa là toàn bộ nội dung sẽ được lưu trữ trong bộ nhớ. Điều này sẽ hoạt động tốt cho các tệp nhỏ

Nhưng có một số trường hợp bạn có thể hưởng lợi từ việc sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7

Tham số tệp với
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}

Xác định tham số tệp với loại

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}

Sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 có một số lợi thế so với
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9

  • Bạn không cần phải sử dụng
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    36 trong giá trị mặc định của tham số
  • Nó sử dụng một tập tin "spooled"
    • Một tệp được lưu trữ trong bộ nhớ với giới hạn kích thước tối đa và sau khi vượt qua giới hạn này, nó sẽ được lưu trữ trong đĩa
  • Điều này có nghĩa là nó sẽ hoạt động tốt đối với các tệp lớn như hình ảnh, video, tệp nhị phân lớn, v.v. không tốn hết bộ nhớ
  • Bạn có thể lấy siêu dữ liệu từ tệp đã tải lên
  • Nó có giao diện
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    37 giống như tệp
  • Nó hiển thị một đối tượng Python
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    38 thực tế mà bạn có thể chuyển trực tiếp đến các thư viện khác mong đợi một đối tượng giống như tệp

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 có các thuộc tính sau

  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    81. Một
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    82 với tên tệp gốc đã được tải lên [e. g.
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    83]
  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    84. Một
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    82 với loại nội dung [loại MIME / loại phương tiện] [e. g.
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    86]
  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    87. Một
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    38 [một đối tượng giống như tệp]. Đây là tệp Python thực tế mà bạn có thể chuyển trực tiếp đến các chức năng hoặc thư viện khác mong đợi một đối tượng "giống như tệp"

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 có các phương pháp
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
37 sau. Tất cả chúng đều gọi các phương thức tệp tương ứng bên dưới [sử dụng _____138 bên trong]

  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    92. Ghi
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    93 [
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    82 hoặc
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    9] vào tệp
  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    96. Đọc
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    97 [
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    98] byte/ký tự của tệp
  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    99. Chuyển đến vị trí byte
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    70 [
    from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    98] trong tệp
    • E. g. ,
      from fastapi import FastAPI, File, UploadFile
      
      app = FastAPI[]
      
      
      @app.post["/files/"]
      async def create_file[file: bytes = File[]]:
          return {"file_size": len[file]}
      
      
      @app.post["/uploadfile/"]
      async def create_upload_file[file: UploadFile]:
          return {"filename": file.filename}
      
      72 sẽ chuyển đến đầu tệp
    • Điều này đặc biệt hữu ích nếu bạn chạy
      from fastapi import FastAPI, File, UploadFile
      
      app = FastAPI[]
      
      
      @app.post["/files/"]
      async def create_file[file: bytes = File[]]:
          return {"file_size": len[file]}
      
      
      @app.post["/uploadfile/"]
      async def create_upload_file[file: UploadFile]:
          return {"filename": file.filename}
      
      73 một lần và sau đó cần đọc lại nội dung
  • from fastapi import FastAPI, File, UploadFile
    
    app = FastAPI[]
    
    
    @app.post["/files/"]
    async def create_file[file: bytes = File[]]:
        return {"file_size": len[file]}
    
    
    @app.post["/uploadfile/"]
    async def create_upload_file[file: UploadFile]:
        return {"filename": file.filename}
    
    74. Đóng tệp

Vì tất cả các phương pháp này là phương pháp

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
37, bạn cần "chờ đợi" chúng

Ví dụ: bên trong hàm vận hành đường dẫn

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
37, bạn có thể lấy nội dung bằng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
3

Nếu bạn đang ở trong một chức năng vận hành đường dẫn

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
77 bình thường, bạn có thể truy cập trực tiếp vào
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
78, chẳng hạn

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
8

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
37 Chi tiết kỹ thuật

Khi bạn sử dụng các phương thức

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
37, FastAPI sẽ chạy các phương thức tệp trong nhóm luồng và chờ chúng

Chi tiết kỹ thuật của Starlette

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 của FastAPI kế thừa trực tiếp từ
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 của Starlette, nhưng thêm một số phần cần thiết để làm cho nó tương thích với Pydantic và các phần khác của FastAPI

"Dữ liệu Biểu mẫu"¶

Cách biểu mẫu HTML [

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
23] gửi dữ liệu đến máy chủ thường sử dụng mã hóa "đặc biệt" cho dữ liệu đó, nó khác với JSON

FastAPI sẽ đảm bảo đọc dữ liệu đó từ đúng nơi thay vì JSON

chi tiết kỹ thuật

Dữ liệu từ các biểu mẫu thường được mã hóa bằng cách sử dụng "loại phương tiện"

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
24 khi nó không bao gồm các tệp

Nhưng khi biểu mẫu bao gồm các tệp, nó được mã hóa thành

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
25. Nếu bạn sử dụng
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2, FastAPI sẽ biết nó phải lấy các tệp từ đúng phần nội dung

Nếu bạn muốn đọc thêm về các mã hóa và trường biểu mẫu này, hãy truy cập tài liệu web MDN cho

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
27

Cảnh báo

Bạn có thể khai báo nhiều tham số

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2 và
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
1 trong thao tác đường dẫn, nhưng bạn cũng không thể khai báo các trường
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
0 mà bạn muốn nhận dưới dạng JSON, vì yêu cầu sẽ có phần thân được mã hóa bằng cách sử dụng
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
25 thay vì
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
12

Đây không phải là hạn chế của FastAPI, nó là một phần của giao thức HTTP

Tải lên tệp tùy chọn¶

Bạn có thể tạo tệp tùy chọn bằng cách sử dụng chú thích loại tiêu chuẩn và đặt giá trị mặc định là

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
13

Trăn 3. 6 trở lênPython 3. 10 trở lên

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 với Siêu dữ liệu bổ sung¶

Ví dụ, bạn cũng có thể sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
36 với
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 để đặt siêu dữ liệu bổ sung

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2

Tải lên nhiều tệp¶

Có thể tải lên nhiều tệp cùng một lúc

Chúng sẽ được liên kết với cùng một "trường biểu mẫu" được gửi bằng "dữ liệu biểu mẫu"

Để sử dụng điều đó, hãy khai báo một danh sách

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9 hoặc
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7

Trăn 3. 6 trở lênPython 3. 9 trở lên

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
1

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2

Bạn sẽ nhận được, như đã khai báo, một

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
19 của
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9 hoặc
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7

chi tiết kỹ thuật

Bạn cũng có thể sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
22

FastAPI cung cấp

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
23 giống như
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
24 để thuận tiện cho bạn, nhà phát triển. Nhưng hầu hết các phản hồi có sẵn đến trực tiếp từ Starlette

Tải lên nhiều tệp với siêu dữ liệu bổ sung¶

Và giống như trước đây, bạn có thể sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
36 để đặt các tham số bổ sung, ngay cả đối với
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7

Trăn 3. 6 trở lênPython 3. 9 trở lên

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
0

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
1

Tóm tắt lại¶

Sử dụng

from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
2,
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
9 và
from fastapi import FastAPI, File, UploadFile

app = FastAPI[]


@app.post["/files/"]
async def create_file[file: bytes = File[]]:
    return {"file_size": len[file]}


@app.post["/uploadfile/"]
async def create_upload_file[file: UploadFile]:
    return {"filename": file.filename}
7 để khai báo các tệp sẽ được tải lên trong yêu cầu, được gửi dưới dạng dữ liệu biểu mẫu

Chủ Đề