Professional Documents
Culture Documents
Probalo Gratis
This tutorial is shared for ajax file upload without refreshing the page. You will
learn to upload different types of file formats (like jpg, png, gif, pdf, docs, zip, text
& more) with some steps that are very simple to understand.
https://codingstatus.com/ajax-file-upload/ 1/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
Contents [show]
myproject
|__uploads/
|__ajax-script.js
|__backend-script.php
|__upload-form.php
Learn Also –
https://codingstatus.com/ajax-file-upload/ 2/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
<title>AJAX File Upload</title>
</head>
<body>
<div id="alerBox"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script type="text/javascript" src="ajax-script.js"></script>
</body>
</html>
$(document).on('submit','#uploadFile',function(e){
e.preventDefault();
var formData = new FormData(this);
$.ajax({
method:"POST",
url: "backend-script.php",
data:formData,
cache:false,
contentType: false,
processData: false,
beforeSend:function(){
$('button[type="submit"]').attr('disabled','disabled');
},
success: function(data){
$('button[type="submit"]').removeAttr('disabled');
$('#alertBox').html(data).fadeIn();
}
});
});
If you want to remove the alert message from the div id="alertBox" after three
seconds, you will have to write the following script in backend-script.php file
window.setInterval(function(){
if ($('#alertBox').css("display") == "block") {
$('#alertBox').fadeOut(); }
https://codingstatus.com/ajax-file-upload/ 4/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
}, 3000);
<?php
upload_file();
function upload_file(){
$uploadTo = "uploads/";
$allowFileType = array('jpg','png','jpeg','gif','pdf','doc');
$fileName = $_FILES['file']['name'];
$tempPath=$_FILES["file"]["tmp_name"];
$basename = basename($fileName);
$originalPath = $uploadTo.$basename;
$fileType = pathinfo($originalPath, PATHINFO_EXTENSION);
if(!empty($fileName)){
if(in_array($fileType, $allowFileType)){
// Upload file to server
if(move_uploaded_file($tempPath,$originalPath)){
echo $fileName." was uploaded successfully";
// write here sql query to store image name in database
}else{
echo 'File Not uploaded ! try again';
}
}else{
https://codingstatus.com/ajax-file-upload/ 5/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
echo $fileType." file type not allowed";
}
}else{
echo "Please Select a file";
}
}
?>
Tutorial Summary
I have provided the standard Ajax File Upload Script. I hope, this tutorial will be
helpful for you. If you have any doubt or questions, ask me directly through the
comment box. I will definitely reply as soon as possible
productos puedan
encontrarlos rápido y
Suscríbase
fácilmente
Stayed Connect
Leave a Reply
Your email address will not be published. Required fields are marked *
https://codingstatus.com/ajax-file-upload/ 6/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I
comment.
POST COMMENT
FOLLOW US
https://codingstatus.com/ajax-file-upload/ 7/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
RECENT POSTS
INTERVIEW QUESTIONS
Home About Us Privacy Policy Disclaimer Terms & Conditions Sitemap Contact Us
Copyright © 2021 CodingStatus - All Rights Reserved
https://codingstatus.com/ajax-file-upload/ 8/9
29/5/2021 Ajax File Upload - Without Refreshing Page - CodingStatus
https://codingstatus.com/ajax-file-upload/ 9/9