Như đã đề cập trước đây ở đây, cũng như ở đây và ở đây, người ta nên sử dụng
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
4 để tải lên các tệp từ các ứng dụng khách [để đọc/ghi import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
5 hãy xem câu trả lời này]. Ví dụNội dung chính Hiển thị
- lựa chọn 1
- Selection 2
- lựa chọn 1
- Selection 2
Phía sau máy chủ
@app.post["/upload"]
def upload[file: UploadFile = File[...]]:
try:
contents = file.file.read[]
with open[file.filename, 'wb'] as f:
f.write[contents]
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close[]
return {"message": f"Successfuly uploaded {file.filename}"}
phía khách hàng
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
Selection 2
Tuy nhiên, nếu bạn vẫn cần gửi cấu hình ảnh đã được mã hóa
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thànhimport requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0 và gửi nó bằng yêu cầuimport requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
2 như sauphía khách hàng
import base64
import requests
url = '//127.0.0.1:8000/upload'
with open["photo.png", "rb"] as image_file:
encoded_string = base64.b64encode[image_file.read[]]
payload ={"filename": "photo.png", "filedata": encoded_string}
resp = requests.post[url=url, data=payload]
Selection 2
Phía sau máy chủ
lựa chọn 1
Như đã đề cập trước đây ở đây, ở đây và ở đây, người ta nên sử dụng
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
4, để tải lên các tệp từ các ứng dụng khách. Ví dụPhía sau máy chủ
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0phía khách hàng
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
Selection 2
Tuy nhiên, nếu bạn vẫn cần gửi cấu hình ảnh đã được mã hóa
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thànhimport requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0 và gửi nó bằng yêu cầuimport requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
2 như sauphía khách hàng
import base64
import requests
url = '//127.0.0.1:8000/upload'
with open["photo.png", "rb"] as image_file:
encoded_string = base64.b64encode[image_file.read[]]
payload ={"filename": "photo.png", "filedata": encoded_string}
resp = requests.post[url=url, data=payload]
Selection 2
Phía sau máy chủ
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
3ổ cắm. on ['data', function [data] { //nghe trên máy khách phát ra 'data'
Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong trình soạn thảo để xem các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều