I want to be able to upload image with AJAX capability asyncronously. I have visited a lot of websites about uploading image with AJAX and I have tried a lot of combination of codes, but although the people who shared the solution said that it was running properly, I think all of them is rubbish. Because, I couldn't upload any image with AJAX without submit event. I want to emphasize this point that I can already upload the image with button which is type of form submitting like in here: AJAX Uploading in StackOverflow But I don't want it. I just want to use uploading image process without submitting the form so that I can use this image inside my blog editor interface before submitting the article. I believe that this is possible, because this is AJAX and we are in 2015. But I need an answer saying this is possible and I need a solution or a way which is going to the solution. Here is the code which I extracted from my blog site and I would like to express it is the code that is the closest solution. Because, in this stackoverflow question, there is a powerful rate which indicate that this is useful:(By the way, I don't want uploading plugin, I know there are too many plugin which does this work. But I want to do by hard-coding and by myself, not by means of a plugin)
JS:
<script>
$(document).ready(function() {
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'bilesenler/yaziEkle/ajaxUpload.php', //Server script to process data
type: 'POST',
enctype: 'multipart/form-data',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // It makes progressBar full.
}
return myXhr;
},
success: function(result){
$("#div1").html(result); // This works very well.
},
error: function(error){
$("#div2").html(error); // This is not being running after the uploading process. So there is no problem.
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
function progressHandlingFunction(e){
if(e.lengthComputable){
$('progress').attr({value:e.loaded,max:e.total});
}
}
});
</script>
HTML:
<form action="../adminPaneli/yaziEkle.php" method="POST" name="addText" enctype="multipart/form-data">
<!--
There are some html codes...
-->
<input type="button" id="upload" value="Upload">
<input type="file" name="file" id="file">
<progress></progress>
<!--
There are some html codes...
-->
<div id="div1"></div><br>
<div id="div2"></div>
<input type="submit" name="publishArticle" value="Publish">
</form>
PHP:
<?php
if (file_exists("../kitaplik/resimler/upload/" . $_FILES["file"]["name"])){
echo $fileName . " already exists.<br>";
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"], "../kitaplik/resimler/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../kitaplik/resimler/upload/" . $_FILES["file"]["name"] . " <br>";
}
?>
When I click the Upload button, the progress bar is being full. That is, the upload process exist. Then, the server is responding the request, and the html page is showing this output:
Notice: Undefined index: file in /.../bilesenler/yaziEkle/ajaxUpload.php on line 1
Notice: Undefined index: file in /.../bilesenler/yaziEkle/ajaxUpload.php on line 5
Notice: Undefined index: file in /.../bilesenler/yaziEkle/ajaxUpload.php on line 5
Notice: Undefined index: file in /.../bilesenler/yaziEkle/ajaxUpload.php on line 6
Stored in: ../kitaplik/resimler/upload/
That is, the "success" label of $.ajax ran, not "error" label. Because, when I remove the error label in $.ajax by making its line comment, the same output appers.
Normally, I can upload image with this server side code with type of submit property. But with ajax capability, receiver of the file, $_FILE, cannot catch the file. Can anybody suggest a idea to solve this problem or any new technique?