将图像从本地存储到API上传

发布于 2025-01-21 08:40:51 字数 828 浏览 1 评论 0原文

假设我有一个拍摄图像并进行一些处理的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文