我正在尝试发送带有一些文字和附件的ajax帖子请求。文件。但是,接受的文件格式是base64binary(它由api指定接受post请求)
使用以下javascript函数将图像文件转换为base64:
function encodeImageFileAsURL(){
var filesSelected = document.getElementById("image").files;
if (filesSelected.length > 0)
{
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result; // <--- data: base64
var newImage = document.createElement('img');
newImage.src = srcData;
convertedimage = srcData;
document.getElementById("imgTest").innerHTML = newImage.outerHTML;
//alert("Converted Base64 version is "+document.getElementById("imgTest").innerHTML);
console.log("Converted Base64 version is "+document.getElementById("imgTest").innerHTML);
}
fileReader.readAsDataURL(fileToLoad);
}
}
使用简单的ajax调用来发送数据。
这是我从api得到的回复: 不能转换数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAABmJLR0QA / WD / AP + gvaeTAAAZDElEQVR4nO3deXhV5Z0H8O / vnJsElE1LRRYd0FqXoK3FEbKo6YxLccFWvZCAC0uWoi3aqe10pu0Y22qn1qVVUUkIWgWSEO1Y6IiK9sGaBbRoRVDbqYoiqBVUQCDLPec3f4AtUsDce5b33Hu + N + fxDx9z3vcr5Hzve5Z7DkBERERERERERERERERERERERERERERERERERERERBElpgOQ / wqTi / IH5m0dCsc9TIDBKtZgAQYrdDCAwbt + SkREB + 29rSpcAFt2 / 6sjwCYFNilkk6hssoB3exLu5pXHbNiI2lo3tP8pCgQLIEsVJW / TA + f1P8FxUCiCo0QwSoGRUIwCMByAFXCEbgBvimCdqrwu0HUAXrUc + 0X7vTf / vHx5bSrg + ckHLIAscOrlv / qM3dVVBJGTLcGJCpwExecA2Kaz7UcXgJcAXSMqL4rg2YPyCp59 / IHLt5sORp / EAoigooo5x0HtYoFbAkgRgOOQ / X9XKQB / BNAuQLvrpFo7WmZuMB0q7rL9lyonlJQ3DFO454vgTFV8GX87Ts9tCrwuwDJVecJN9Hls5YJLt5rOFDcsAEPGld890tZEUi2cD0UxgITpTCYpsF2gy0Wsh3vy8v7nmfuv2Gw6UxywAEJUkmw4Vm23QkWTonKC6TwR5grQ4aq2pNy85mdbpr1jOlCuYgEEbMwFcw7q089OqqIS0BLwzzxd3QCWAlI / 3BnwaEvLRMd0oFzCX8aAjJtYd6ZtS7UqLgDQx3SeHPEugGYRrWtrrF5rOkwuYAH4aEz1nLz8bdYlULlaoGNN58lhDoClCvlFR1Plk6bDZDMWgA / KKuYM7lbrGwBmAjjMdJ6Y + Yuq3tm9Q + tXLanZYTpMtmEBeFCUvHu4JPK + A + h0KPqbzhN zGwG9Pd / pmb285aqPTIfJFiyADJyerB + VsvV ...到System.Byte。 参数名称:type ---&gt;输入字符串的格式不正确。
有人会告诉我解决这个问题的方法或方法吗?
提前谢谢。