如何通过XHR请求向fastAPI发送图像文件

2024-05-28 19:47:55 发布

您现在位置:Python中文网/ 问答频道 /正文

不知道如何通过请求将图像作为base64字符串发送,并将其作为blob对象存储在sqlite FastAPI框架中,也不知道使用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);

Tags: 对象字符串图像框架newsqlitevarblob

热门问题