1

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

1 Answers1

0

You can use FormData().

var fd=new FormData();
fd.append("filename.txt",blob);
xhr.open("POST","url",true);
xhr.send(fd);
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • By using this method can I send other text info's gathered by input fields in submit form ?..... – Mohammed Fareedh Jan 11 '21 at 14:38
  • I think yes you can. For example you have an Form with text inputfields and file upload. with formdata you can collect all data and pass to the request. – Maik Lowrey Jan 11 '21 at 16:51