A simple Ajax upload progress show sample _ajax related

Source: Internet
Author: User
Tags file upload
This example uses jquery.form.js, please go to the demo page to view

CSS Code
Copy Code code as follows:

<style>
form {display:block; margin:20px auto; background: #eee; border-radius:10px; padding:15px}
#progress {position:relative; width:400px; border:1px solid #ddd; padding:1px; border-radius:3px;}
#bar {background-color: #B4F5B4; width:0%; height:20px; border-radius:3px;}
#percent {position:absolute; display:inline-block; top:3px; left:48%;}
</style>

Xml/html Code
Copy Code code as follows:

<form id= "MyForm" action= "upload.php" method= "post" enctype= "Multipart/form-data" >
<input type= "File" size= "Name=" "MyFile" >
<input type= "Submit" value= "Ajax File Upload" >
</form>
<div id= "Progress" >
<div id= "Bar" ></div>
<div id= "percent" >0%</div >
</div>
<div id= "Message" ></div>

JavaScript Code
Copy Code code as follows:

<script>
$ (document). Ready (function ()
{
var options = {
Beforesend:function ()
{
$ ("#progress"). Show ();
Clear everything
$ ("#bar"). Width (' 0% ');
$ ("#message"). HTML ("");
$ ("#percent"). HTML ("0%");
},
Uploadprogress:function (event, Position, total, PercentComplete)
{
$ ("#bar"). Width (percentcomplete+ '% ');
$ ("#percent"). html (percentcomplete+ '% ');
},
Success:function ()
{
$ ("#bar"). Width (' 100% ');
$ ("#percent"). html (' 100% ');
},
Complete:function (response)
{
$ ("#message"). HTML ("<font color= ' green ' >" +response.responsetext+ "</font>");
},
Error:function ()
{
$ ("#message"). HTML ("<font color= ' Red ' > error:unable to upload files</font>");
}
};
$ ("#myForm"). Ajaxform (options);
});
</script>

upload.php
Copy Code code as follows:

<?php
$output _dir = ". /upload/";
if (Isset ($_files["myfile"]))
{
Filter the file types, if you want.
if ($_files["myfile"] ["error"] > 0)
{
echo "Error:". $_files["File" ["Error"]. "<br>";
}
Else
{
Move the uploaded file to uploads folder;
Move_uploaded_file ($_files["myfile"] ["Tmp_name"], $output _dir "$_files["] ["name"]);
echo "uploaded File:". $_files["MyFile" ["Name"];
}
}
?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.