一个简单的ajax上传 上传进度显示

前端之家收集整理的这篇文章主要介绍了一个简单的ajax上传 上传进度显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本例用了jquery.form.js请到演示页面查看

CSS Code
  1. <style>
  2. form{display:block;margin:20pxauto;background:#eee;border-radius:10px;padding:15px}
  3. #progress{position:relative;width:400px;border:1pxsolid#ddd;padding:1px;border-radius:3px;}
  4. #bar{background-color:#B4F5B4;width:0%;height:20px;border-radius:3px;}
  5. #percent{position:absolute;display:inline-block;top:3px;left:48%;}
  6. </style>

XML/HTML Code

    <formid="myForm"action="upload.PHP"method="post"enctype="multipart/form-data">
  1. inputtype="file"size="60"name="myfile">
  2. inputtype="submit"value="AjaxFileUpload">
  3. </form>
  4. divid="progress">
  5. divid="bar"></div>
  6. divid="percent">0%</div>
  7. div>
  8. <div id="message"></div>

JavaScript Code

    <script>
  1. $(document).ready(function()
  2. {
  3. varoptions={
  4. beforeSend:function()
  5. $("#progress").show();
  6. //cleareverything
  7. $("#bar").width('0%');
  8. $("#message").html("");
  9. $("#percent").html("0%");
  10. },
  11. uploadProgress:function(event,position,total,percentComplete)
  12. $("#bar").width(percentComplete+'%');
  13. $("#percent").html(percentComplete+'%');
  14. success:function()
  15. {
  16. $("#bar").width('100%');
  17. $("#percent").html('100%');
  18. complete:function(response)
  19. $("#message").html("<fontcolor='green'>"+response.responseText+"</font>");
  20. error:function()
  21. $("#message").html("<fontcolor='red'>ERROR:unabletouploadfiles</font>");
  22. }
  23. };
  24. $("#myForm").ajaxForm(options);
  25. });
  26. </script>

upload.PHP

PHP Code

    <?PHP
  1. $output_dir="../upload/";
  2. if(isset($_FILES["myfile"]))
  3. //Filterthefiletypes,ifyouwant.
  4. if($_FILES["myfile"]["error"]>0)
  5. echo"Error:".$_FILES["file"]["error"]."<br>";
  6. else
  7. //movetheuploadedfiletouploadsfolder;
  8. move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$_FILES["myfile"]["name"]);
  9. echo"UploadedFile:".$_FILES["myfile"]["name"];
  10. }
  11. ?>


原文地址:http://www.freejs.net/article_biaodan_92.html

原文链接:https://www.f2er.com/ajax/165577.html

猜你在找的Ajax相关文章