简体   繁体   English

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

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

Dont know how to send image through request as base64 string to store it as blob object in sqlite FastAPI framework and has no idea about which method to use, using formdata or???不知道如何通过请求将图像发送为 base64 字符串以将其存储为 sqlite FastAPI 框架中的 Blob object 并且不知道使用哪种方法,使用 formdata 或??? is there any method to do above mentioned task有什么方法可以完成上述任务

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);

You can use FormData().您可以使用 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