我试图从ajax帖子转换成Base64字符串, 并且该请求似乎运行良好-获得成功的回报。
但是当我尝试将其转换回图像时,出现此错误:
mscorlib.dll中发生了'System.FormatException'类型的异常,但未在用户代码中处理
其他信息:输入不是有效的Base-64字符串,因为它包含非base 64字符,两个以上的填充字符或填充字符中的非法字符。
[WebMethod(EnableSession = true)]
public static string insertFile(string base64File)
{
string path = HttpContext.Current.Server.MapPath("~/Users/Organizer/organizersData/"); // de path
//Check if directory exist
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path); //Create directory if it doesn't exist
}
string imageName = "event1" + ".jpg";// for instance
//set the image path
string imgPath = Path.Combine(path, imageName);
string convert = base64File.Replace("data:image/png;base64,", String.Empty).Replace("data:image/jpg;base64,", String.Empty).Replace("data:image/bmp;base64,", string.Empty); ;
string converted = convert.Replace('-', '+');
converted = converted.Replace('_', '/');
byte[] imageBytes = Convert.FromBase64String(converted);
File.WriteAllBytes(imgPath, imageBytes); //write the file in the directory
return "success";
}
这是base64字符串。
数据:text / html的; BASE64,PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBYSFRNTCAxLjAgU3RyaWN0Ly9FTiIgImh0dHA6Ly93d3cudzMub3JnL1RSL3hodG1sMS9EVEQveGh0bWwxLXN0cmljdC5kdGQiPiAKPGh0bWwgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGh0bWwiPiAKPGhlYWQ + IAo8dGl0bGU + SUlTIDEwLjAgRGV0YWlsZWQgRXJyb3IgLSA0MDQuMCAtIE5vdCBGb3VuZDwvdGl0bGU + IAo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPiAKPCEtLSAKYm9keXttYXJnaW46MDtmb250LXNpemU6LjdlbTtmb250LWZhbWlseTpWZXJkYW5hLEFyaWFsLEhlbHZldGljYSxzYW5zLXNlcmlmO30gCmNvZGV7bWFyZ2luOjA7Y29sb3I6IzAwNjYwMDtmb250LXNpemU6MS4xZW07Zm9udC13ZWlnaHQ6Ym9sZ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+ PHRkPiZuYnNwOyZuYnNwOyZuYnNwO0M6XFVzZXJzXERWWVJBXERvY3VtZW50c1xJSVNFeHByZXNzXFRyYWNlTG9nRmlsZXNcUkVHRUFTRUJFVEFWMS4wPC90ZD48L3R yPiAKICAgPC90YWJsZT4gCiAgIDxkaXYgY2xhc3M9ImNsZWFyIj48L2Rpdj4gCiAgPC9kaXY + IAogPC9maWVsZHNldD4gCjwvZGl2PiAKIAo8ZGl2IGNsYXNzPSJjb250ZW50LWNvbnRhaW5lciI + IAogPGZpZWxkc2V0PjxoND5Nb3JlIEluZm9ybWF0aW9uOjwvaDQ + IAogIFRoaXMgZXJyb3IgbWVhbnMgdGhhdCB0aGUgZmlsZSBvciBkaXJlY3RvcnkgZG9lcyBub3QgZXhpc3Qgb24gdGhlIHNlcnZlci4gQ3JlYXRlIHRoZSBmaWxlIG9yIGRpcmVjdG9yeSBhbmQgdHJ5IHRoZSByZXF1ZXN0IGFnYWluLiAKICA8cD48YSBocmVmPSJodHRwczovL2dvLm1pY3Jvc29mdC5jb20vZndsaW5rLz9MaW5rSUQ9NjIyOTMmYW1wO0lJUzcwRXJyb3I9NDA0LDAsMHg4MDA3MDAwMiwxNzEzNCI + VmlldyBtb3JlIGluZm9ybWF0aW9uICZyYXF1bzs8L2E + PC9wPiAKICAgCiA8L2ZpZWxkc2V0PiAKPC9kaXY + IAo8L2Rpdj4gCjwvYm9keT4gCjwvaHRtbD4gCg ==
答案 0 :(得分:0)
我真的不知道我要转换html:D
感谢@john和@Access Denied指出错误。.
非常感谢您的帮助:)
这是我用于转换图像的js脚本。
function readFile() {
if (this.files && this.files[0]) {
var FR = new FileReader();
FR.addEventListener("load", function (e) {
sessionStorage.setItem("imagebase64", e.target.result);
});
FR.readAsDataURL(this.files[0]);
}
}
document.getElementById("test").addEventListener("change", readFile);
答案 1 :(得分:0)
在HTML中为图片标签设置正确的来源
src="data:image/jpeg;base64,HERE_IS_YOUR_BASE64STRING"