我正在使用javascript,我得到一个图像文件并将其编码为基础64,如此
function el(id){return document.getElementById(id);} // Get elem by ID
function readImage() {
if ( this.files && this.files[0] ) {
var FR= new FileReader();
FR.onload = function(e) {
el("img").src = e.target.result;
el("base").innerHTML = e.target.result;
};
FR.readAsDataURL( this.files[0] );
}
}
el("asd").addEventListener("change", readImage, false);
编码文本是这样的,看起来像这样
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4gxYSUNDX1BST0ZJTE.........
现在我想用一个我知道怎么做的帖子发送这些数据,但是当我读到我在另一边发送的数据时,我只看到data:image/jpeg
,Base64编码的起始位置和方式我可以把它作为字符串传递吗?
我像这样发帖子
var base64data;
//base64data is declared in the function readImage and I log it in this one and it has data
function callHttpReq () {
var http = new XMLHttpRequest();
var url = "/admin/website_sql/save_info.py";
var params = "website_photo=" + base64data;
http.open("POST", url, true);
console.log(base64data);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(params);
由于