簡體   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