previously while developing systems whenever i faced a scenario of uploading images to the server through ajax i often use different plugins but now i have to create an interface that is to customized that i have to make everything from scratch!
i have an input type file on my page and what i am doing is i am uploading the image to server via ajax!
now i have developed a snippet let me show you the code first!
html
<form method="post" id="myform" action="#">
<div class="image" id="upload-parent">
<input id="upload-image" type="file" name="myfile" accept="image/x-png, image/gif, image/jpeg, image/jpg"/>
<label for="upload-image">+</label>
</div>
</form>
Jquery/Javascript
$("#upload-image").change(function(){
readURL(this);
// getting file size and type
// console.log(this.files[0].type);
// console.log(this.files[0].size);
// size in bytes /124 for kb
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$append='<div class="image">'+
'<img src="'+reader.result+'" class="img-responsive"/>'+
'</div>';
$($append).insertBefore("#right-panel .social #max-width #social #upload #images-block #upload-parent");
$.ajax({
type: 'post',
url: 'upload_file_test.php',
data: new FormData($("#myform")[0]),
contentType:false,
processData:false,
success: function (data) {
}
});
};
reader.readAsDataURL(input.files[0]);
}
}
PHP
<?php
if(isset($_FILES['myfile']['name'])){
echo "set";
$target = "uploads/"; //make sure to create a folder named 'uploads' and put it in the same directory that upload.php (this script) is in
$file_name = $_FILES['myfile']['name'];
$tmp_dir = $_FILES['myfile']['tmp_name'];
if(!preg_match('/(gif|jpe?g|png|csv)$/i', $file_name) //set permissible file types
)
{
echo "sorry that file type is not allowed";
} else {
move_uploaded_file($tmp_dir, $target . $file_name);
}
}
else
{
echo "not set";
}
?>
this code is working 100% fine in case someone else is facing problems creating the above scenarios!
what this snippet is doing is on change of image upload it is previewing the image and then uploading the image on the server.
The issue!
As you can see i am using the form data object to send the image to backend now for this particular scenario i will always need to have a form inside page and more this object method might create issue when input type file are being dynamically generated! or lets suppose i have 50 input type files and i only want to send a specific input type file to backend not all of them! because right now it will send everything to the server which is never an efficient approach if i only want to send one specific file why send everything to the backend?
i did researched alot about it and i can see that the formdata object does alot of things for us out of the box for image processing! what i want is to get the specific input file(Most probably with file reader API) and process it and send only that specific item to the backend! this is what i am trying to accomplish here!
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$append='<div class="image">'+
'<img src="'+reader.result+'" class="img-responsive"/>'+
'</div>';
$($append).insertBefore("#right-panel .social #max-width #social #upload #images-block #upload-parent");
$.ajax({
type: 'post',
url: 'upload_file_test.php',
data: "myfile="+reader.result,
contentType:false,
processData:false,
success: function (data) {
}
});
};
reader.readAsDataURL(input.files[0]);
}
}
after executing the block of code it just shows me the paraemters to be object file and php is unable to parse it!
can someone guide me how can we send an independent image to the backend without using formdata object?