我是否正确地以 base64 编码图像?
Am I encoding images in base64 correctly?
所以我正在发送一个服务器,我正在处理 json 数组中的一组数据。我正在使用 stringify 来做到这一点。当我发送没有图像的数组时,它发送成功。但是,服务器要求我以 base64 编码发送图像。
所以我做了一些阅读,发现我可以使用 canvas 来编码我的图像。但是,我收到 500 Internal Server Error 代码。我知道该字符串正在发送 base64 编码,因为我已将它在发送前打印到控制台。我尝试的一件事是从字符串中取出前几组字符,比如 "data:image png base64" 或 "data:image jpg base64"。我知道当它打印到控制台时我正确地从字符串中取出它。无论我是否包含它,服务器仍然向我发送错误。
顺便说一句,我发送的是带ajax的字符串。所以这是我将代码更改为 base64 的代码。这是该代码的一个片段:
$(function()
{
var file = document.getElementById("image_load").src;
var draw = document.getElementById("image_load");
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.drawImage(draw, 0, 0);
var picture= canvas.toDataURL();
//picture = picture.replace(/^data:image\/(png|jpg);/, "");
//picture = picture.replace(/^data:image\/(png|jpg);base64,/, "");
//the first removes everything but base64 from the beggining of the string
});
//500 (Internal Server Error)
//data:image/png;base64,
如果有帮助,我可以 post 一些 base64 编码。
"500 内部服务器错误代码" - 是服务器端错误。因此,您应该在服务器端寻找问题。接收脚本可能需要不同 json 格式的数据。
所以我正在发送一个服务器,我正在处理 json 数组中的一组数据。我正在使用 stringify 来做到这一点。当我发送没有图像的数组时,它发送成功。但是,服务器要求我以 base64 编码发送图像。
所以我做了一些阅读,发现我可以使用 canvas 来编码我的图像。但是,我收到 500 Internal Server Error 代码。我知道该字符串正在发送 base64 编码,因为我已将它在发送前打印到控制台。我尝试的一件事是从字符串中取出前几组字符,比如 "data:image png base64" 或 "data:image jpg base64"。我知道当它打印到控制台时我正确地从字符串中取出它。无论我是否包含它,服务器仍然向我发送错误。
顺便说一句,我发送的是带ajax的字符串。所以这是我将代码更改为 base64 的代码。这是该代码的一个片段:
$(function()
{
var file = document.getElementById("image_load").src;
var draw = document.getElementById("image_load");
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.drawImage(draw, 0, 0);
var picture= canvas.toDataURL();
//picture = picture.replace(/^data:image\/(png|jpg);/, "");
//picture = picture.replace(/^data:image\/(png|jpg);base64,/, "");
//the first removes everything but base64 from the beggining of the string
});
//500 (Internal Server Error)
//data:image/png;base64,
如果有帮助,我可以 post 一些 base64 编码。
"500 内部服务器错误代码" - 是服务器端错误。因此,您应该在服务器端寻找问题。接收脚本可能需要不同 json 格式的数据。