将图像从本地存储到API上传
假设我有一个拍摄图像并进行一些处理的API。示例:
@app.post("/upload/")
async def upload_image(file: UploadFile = File(...)):
file_content = await file.read()
print(file_content)
extension = file.filename.split(".")[1]
if extension not in ["png", "jpg"]:
return {"status": "error", "detail": "File is not supported"}
ocr_result = ocr(file_content)
return {"filename": file.filename, "text": ocr_result}
我想通过Axios从我的本地存储中上传图像来对此进行发布。所以我这样做了:
let file = fs.createReadStream('discount.jpg')
let data = new FormData();
data.append('file', file, 'discount.jpg');
axios.post('http://127.0.0.1:8000/upload/', data, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(function() {
console.log(response.data);
})
但这似乎不起作用。我对我做错了什么想法?
Suppose I have an api that takes an image and does some processing. Example:
@app.post("/upload/")
async def upload_image(file: UploadFile = File(...)):
file_content = await file.read()
print(file_content)
extension = file.filename.split(".")[1]
if extension not in ["png", "jpg"]:
return {"status": "error", "detail": "File is not supported"}
ocr_result = ocr(file_content)
return {"filename": file.filename, "text": ocr_result}
And I want to make a post request to this using axios by uploading an image from my local storage. So I did this:
let file = fs.createReadStream('discount.jpg')
let data = new FormData();
data.append('file', file, 'discount.jpg');
axios.post('http://127.0.0.1:8000/upload/', data, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(function() {
console.log(response.data);
})
But this doesn't seem to be working. Any idea on what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论