0

I have a HTML file with some images as below:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Chapter 4 : Data Types - Data URI </title>
  <script type="text/javascript"></script>
</head>
<body>
  <input type="button" value="Test" text="Button" onclick="UploadStart()"/>
  <img id="tst" src="1.png">
  <img id="tst" src="2.png">
</body>

From this Page i am taking the images with Javascript as below :

UploadStart Function

function UploadStart(){
  if (window.File && window.FileReader && window.FileList && window.Blob) {
      sendRequest(MakeBase64());          
  } else {
      alert('The File APIs are not fully supported in this browser.');
  }
}

Base64 Conversion Method

function MakeBase64(){
  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d"); 
  var img = document.getElementById("tst");
  ctx.drawImage(img, 20, 20);
  var dataURL = c.toDataURL("image/png");
  var blob = dataURItoBlob(dataURL);
  return blob;
}

sendRequest

function sendRequest(data)
{
   var xhr = createXHR();
   if (xhr){
   var url = "http://localhost/AjaxFormProcessing/AJAXIMG/server.php";
   var payload = "image=" + data;
   xhr.open("POST",url + "?" + payload,true);
   xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded ');     
   xhr.onreadystatechange = function(){handleResponse(xhr);};
   xhr.send(null);
 }
 }

Server.php

 <?php 
$data = $_REQUEST['image'];
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data=base64_decode($data); 
$file = 'uploads/'. uniqid() . '.png';
$success = file_put_contents($file, $data);
if($success){echo "Success";}
else {echo "Failed";}
?>

I am getting Success on my Client Side. But The image getting saved is only 1KB always and has no data in it. Means it's blank image. What's wrong going on ??

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55
  • Are you familiar with the RK's [jQuery File Upload plugin](http://hayageek.com/docs/jquery-upload-file.php#)? Also, remember that each image stored in the database will increase the size of the database. Backups will become huge. Might be best to save the image to the file system and store only the file name (and possibly the location on your server) in the DB. [See this SO answer](http://stackoverflow.com/questions/32253687/java-script-auto-refresh-update-function-but-getting-one-error/32253932#32253932) for more about AJAX. – cssyphus Sep 09 '15 at 18:22
  • @gibberish I dont want to save on database. I am trying to store in file system. I think you didn't read my problem carefully :( – sohel14_cse_ju Sep 09 '15 at 18:26

0 Answers0