js实现图片上传预览及进度圆圈

完整案例下载地址

最近在做图片上传的时候,上网找了比较久还没有现成的,因此自己做了一个,实现的功能如下:

1.去除”input file”默认的样式;

2.使用上传可以查看上传进度(本博做的是上传的百分比,做成进度圆圈类似);

3.图片从本地选择上传后预览图片;

首先是去除浏览器默认上传图片框,通过设置css样式可以简单的修改成想要的效果。

index.html代码如下;

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. body{
  8. text-align: center;
  9. }
  10. .uploadWrap{
  11. margin: auto;
  12. width: 500px;
  13. height: 500px;
  14. }
  15. #li_idCard{
  16. width: 210px;
  17. height: 120px;
  18. background: #b3dde2;
  19. color: #fff;
  20. text-align: center;
  21. line-height: 120px;
  22. font-size: 30px;
  23. border: 1px solid #999;
  24. position: relative;
  25. }
  26. #IDCardFront{
  27. width: 210px;
  28. height: 120px;
  29. opacity: 0;
  30. position: absolute;
  31. right: 0;
  32. top: 0;
  33. cursor: pointer;
  34. }
  35. .process {
  36. width: 280px;
  37. height: 140px;
  38. position: absolute;
  39. top: 30%;
  40. left: 40%;
  41. display: none;
  42. }
  43. #img{
  44. margin: 0 auto;
  45. margin-top: 200px;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class='uploadWrap'>
  51. <div class="uploadIDcard">
  52. <div class="upload_box">
  53. <ul>
  54. <li id='li_idCard'>
  55. <span class="percent" id="percent1">+</span>
  56. <canvas class="process" id="process1">0%</canvas>
  57. <input type="file" id="IDCardFront" name="IDCardFront">
  58. <img id='img' src="">
  59. </li>
  60. </ul>
  61. </div>
  62. </div>
  63. </div>
  64. </body>
  65. <script type="text/javascript" src="jquery.min.js"></script>
  66. <script type="text/javascript" src="jquery.form.js"></script>
  67. </html>

这里html代码引入外部js文件。

js代码

  1. <script>
  2. //进度条渲染效果
  3. function drawProcess(id) {
  4. var text = $("#" + id).text();
  5. var process = text.substring(0, text.length - 1);
  6. var canvas = $("#" + id);
  7. var context = canvas[0].getContext('2d');
  8. context.clearRect(0, 0, 48, 48);
  9. context.beginPath();
  10. context.moveTo(240, 240);
  11. context.arc(24, 24, 24, 0, Math.PI * 2, false);
  12. context.closePath();
  13. context.fillStyle = '#fff';
  14. context.fill();
  15. context.beginPath();
  16. context.moveTo(24, 24);
  17. context.arc(24, 24, 24, 0, Math.PI * 2 * process / 100, false);
  18. context.closePath();
  19. context.fillStyle = 'rgb(234,117,96)'; //圈的百分比
  20. context.fill();
  21. context.beginPath();
  22. context.moveTo(24, 24);
  23. context.arc(24, 24, 21, 0, Math.PI * 2, true);
  24. context.closePath();
  25. context.fillStyle = 'rgba(255,255,255,1)';
  26. context.fill();
  27. context.beginPath();
  28. context.arc(24, 24, 18.5, 0, Math.PI * 2, true);
  29. context.closePath();
  30. context.strokeStyle = '#ddd';
  31. context.stroke();
  32. context.font = "bold 9pt Arial";
  33. context.fillStyle = 'rgb(234,117,96)'; //百分比数字颜色
  34. context.textAlign = 'center';
  35. context.textBaseline = 'middle';
  36. context.moveTo(24, 24);
  37. context.fillText(text, 24, 24);
  38. }
  39. $("#IDCardFront").change(function () {
  40. if ($("#FrontUpload").length > 0) {
  41. } else {
  42. $("#li_idCard").wrap("<form id='FrontUpload' action='upload.php' method='post' enctype='multipart/form-data'></form>");
  43. }
  44. $("#FrontUpload").ajaxSubmit({
  45. dataType: 'json',
  46. beforeSend: function () {
  47. },
  48. uploadProgress: function (event, position, total, percentComplete) {
  49. $('#percent1').hide();
  50. $('#process1').show();
  51. $('#process1').text(percentComplete + "%");
  52. drawProcess('process1');
  53. },
  54. success: function (data) {
  55. if (data.result == 'true') {
  56. $('#img').attr('src','./'+data.img+'');
  57. } else {
  58. alert('上传失败');
  59. }
  60. }
  61. });
  62. });
  63. </script>

服务器端用的是PHP

upload.php代码:

  1. <?php
  2. error_reporting(0);//禁用错误报告
  3. if (file_exists("./" . $_FILES["IDCardFront"]["name"])){
  4. $arrRet=array(
  5. 'result'=>'false'
  6. );
  7. }else{
  8. $ret=move_uploaded_file($_FILES["IDCardFront"]["tmp_name"],"./" . $_FILES["IDCardFront"]["name"]);
  9. if($ret){
  10. $arrRet=array(
  11. 'result'=>'true',
  12. 'img'=> $_FILES["IDCardFront"]["name"]
  13. );
  14. }else{
  15. $arrRet=array(
  16. 'result'=>'false'
  17. );
  18. }
  19. }
  20. echo json_encode($arrRet);
  21. ?>

实现效果:



评论 0

发表评论

Top