繁体   English   中英

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

[英]How to send an image file through XHR request to fastAPI

不知道如何通过请求将图像发送为 base64 字符串以将其存储为 sqlite FastAPI 框架中的 Blob object 并且不知道使用哪种方法,使用 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM