admin管理员组文章数量:1289557
I'm posting a JPEG file from a HTML5 canvas element as a string to PHP using:
function createJPG() {
var dataUrl = document.getElementById('canvas').childNodes[4].toDataURL("image/jpeg");
console.log(dataUrl);
var params = "theimage=" + dataUrl;
var http = new XMLHttpRequest();
http.open("POST", "/avatar/php/save-avatar.php", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
}
Everything is fine, the dataUrl is correct (in the console) and contains a valid JPEG file. But when I retrieve it with PHP, all the plus-signs are replaced by spaces?
The PHP-code:
$name = "image.jpg";
$data=$_POST['theimage'];
file_put_contents($name, base64_decode(substr($data, strpos($data, ",")+1)));
file_put_contents("../images/avatars/uploads/generated" . rand(0,200) . ".txt", $data);
I use the text file to see what the data looks like since I can't bother to figure out how to echo back to the JS :). The text file contains the exact same data as the JS console logs, but without the +'es and with spaces instead, which results in the JPEG files being corrupt.
What should I do?
I'm posting a JPEG file from a HTML5 canvas element as a string to PHP using:
function createJPG() {
var dataUrl = document.getElementById('canvas').childNodes[4].toDataURL("image/jpeg");
console.log(dataUrl);
var params = "theimage=" + dataUrl;
var http = new XMLHttpRequest();
http.open("POST", "/avatar/php/save-avatar.php", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
}
Everything is fine, the dataUrl is correct (in the console) and contains a valid JPEG file. But when I retrieve it with PHP, all the plus-signs are replaced by spaces?
The PHP-code:
$name = "image.jpg";
$data=$_POST['theimage'];
file_put_contents($name, base64_decode(substr($data, strpos($data, ",")+1)));
file_put_contents("../images/avatars/uploads/generated" . rand(0,200) . ".txt", $data);
I use the text file to see what the data looks like since I can't bother to figure out how to echo back to the JS :). The text file contains the exact same data as the JS console logs, but without the +'es and with spaces instead, which results in the JPEG files being corrupt.
What should I do?
Share Improve this question asked Jan 10, 2012 at 15:30 ReinRein 1,3683 gold badges17 silver badges29 bronze badges2 Answers
Reset to default 13It doesn't remove them. +
means "a space" in application/x-www-form-urlencoded
data. So they get converted to spaces when the URI is decoded on the server.
As always for taking some data and putting it into a URL, you need to encode it:
var params = "theimage=" + encodeURIComponent(dataUrl);
You should urlencode the dataUrl:
var params = "theimage=" + urlencode(dataUrl);
本文标签: javascriptXMLHttpRequest POST to PHP removes plussignsStack Overflow
版权声明:本文标题:javascript - XMLHttpRequest POST to PHP removes plus-signs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741462655a2380128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论