Reply to Re: How to use jQuery ajax instead of XMLHttpRequest

Re: How to use jQuery ajax instead of XMLHttpRequest new!
by Malinga, 11 years, 2 months ago
i tried to upload via jQuery. but it didn't work

my html code
<form id="fileUp" enctype="multipart/form-data" method="post" action="#">
  <input type="file" size="32" id="fileInput" name="image_field" value="">
  <input type="button" >
</form>

my js code
function upload(){
  var filename = $('input[type=file]').val().split('\\').pop();
  //alert(filename);
  var path = $('input[type=file]').val();
  $.ajax({
    headers: { "Cache-Control": "no-cache","X-Requested-With":"XMLHttpRequest","X-File-Name": filename },
    url:'testAjax.php',
    data:{imageName:'image_field',path:path},
    type:'POST',
    success: function(data){
      $('#error').html(data);
    },
  });
}

my php code
require_once('../include/upload.php');
if (isset($_SERVER['HTTP_X_FILE_NAME']) && isset($_SERVER['CONTENT_LENGTH'])) {
  $handle = new Upload('php:'.$_SERVER['HTTP_X_FILE_NAME']);
  if ($handle->uploaded) {
    echo "uploaded";    
  } else{
    echo 'error : ' . $handle->error;
  }
}

output > error : Can't read image source. Not an image?.Reply

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important