javascript - 如何通过 XHR 请求将图像文件发送到 fastAPI
问题描述
不知道如何通过请求以 base64 字符串的形式发送图像以将其存储为 sqlite FastAPI 框架中的 blob 对象,并且不知道使用哪种方法,使用 formdata 或 ??? 有什么方法可以完成上述任务
var pimagefile = document.getElementById("pImage").files[0];
var pimageblob = new Blob([pimagefile],{type: 'image/jpg'});
function convertToBase64(){
var pimageBase64 = // convert to base64 string
}
var toSend = {
pimage: pimageBase64
}
var jsonString = JSON.stringify(toSend);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://127.0.0.1:8000/products/add/", true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(jsonString);
解决方案
您可以使用 FormData()。
var fd=new FormData();
fd.append("filename.txt",blob);
xhr.open("POST","url",true);
xhr.send(fd);
推荐阅读
- node.js - NodeJS 谷歌云服务账户不记名令牌
- javascript - 在 JS 编译时 React DOM 更新。但是刷新浏览器会给出错误'无法读取未定义的属性(读取'1')'
- pdf - 下载 pdf - 屏幕阅读器的辅助功能
- android - Android KeyStore CTS ImportWrappedKeyTest 错误 - INVALID_ARGUMENT(代码 -38)
- serial-port - 不同调制解调器的 RS232 例程有何不同?
- python - 在 OpenCV 中拼接来自两台相机的图像
- python - 使用 pymongo 将数据从 Mysql 迁移到 MongoDB
- java - 带有空值的 Jdbc bigint 数组
- python-3.x - 用户数据脚本完成
- python - Python:从 AWS MediaConvert 中的最后一帧生成缩略图