基于Jquery插件Uploadify实现实时显示进度条上传图片_jquery

小辰同学_4543

小辰同学_4543

2016-05-16

3127人浏览

原创

先了解了解Uploadify,具体内容如下

uploadify是一个简单易用的多文件上传方案。作为一个jquery插件,uploadify使用简单,并具有高度的定制性。

Uploadify特性:

Uploadify简单说来,是基于Jquery的一款文件上传插件。它的功能特色总结如下:

1)、支持单文件或多文件上传,可控制并发上传的文件数
2)、在服务器端支持各种语言与之配合使用,诸如PHP,.NET,Java……
3)、通过参数可配置上传文件类型及大小限制
4)、通过参数可配置是否选择文件后自动上传
5)、易于扩展,可控制每一步骤的回调函数(onSelect, onCancel……)
6)、通过接口参数和CSS控制外观
7)、提供上传进度的事件回调,实时显示上传进度
8)、开始之前要先下载插件安装包到本地并引用,详细实现请看代码注释,下面开始代码。

1、html代码

<div id="webApplogo_file" style="display: block; width: 800px; background-color: #fff;">
      <ul>
<li style="margin-left: 213px;"><span class="black_blod14">LOGO图标:</span></li>
        <li style="margin-left: 3px;">
          <input type="text" readonly id="text_webApplogo" name="app_logo" class="appipt1" value="<%=applogo %>">
</li>
        <li style="padding-top: 1px;">
          <input type="file" id="webApplogo" name="webApplogo">
</li>
        <li><span style="display: none; margin-left: 5px; padding-left: 20px; color: #EA5200;
          font-size: 12px; background: url('Images/icon_03.gif' ) no-repeat 0px 0px;" id="textporApplogo">
          请上传LOGO图标!</span></li>
        <li style="margin-left: 220px;">
<span class="grey999" style="margin-left: 90px; float: left;">
          尺寸72px*72px,png格式,建议使用 图标PSD模板 制作</span>
          <div id="QID_webApplogo" class="fileQueue"></div>
          <table id="webApplogo_tab" width="50%" border="0" cellspacing="0" cellpadding="0" align="center" class="grey999" style="display: none; margin-left: 90px; float: left;"><tr>
<td align="center" valign="bottom">
                @@##@@<br>
                (64*64)
              </td>
              <td align="center" valign="bottom">
                @@##@@<br>
                (48*48)
              </td>
              <td align="center" valign="bottom">
                @@##@@<br>
                (32*32)
              </td>
              <td align="center" valign="bottom">
                @@##@@<br>
                (16*16)
              </td>
            </tr></table>
</li>
      </ul>
</div>

2、Javascript代码(关键部分)

jQuery 1.12.4
jQuery 1.12.4

jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。

下载
$("#webApplogo").uploadify({ 
      'uploader': 'js/uploadify-v2.1.4/uploadify.swf',//进度条,Uploadify里面含有
      'script': 'UploadApplogo.ashx',//一般处理程序
      'cancelImg': 'js/uploadify-v2.1.4/cancel.png',//取消图片路径
      'folder': 'Imagelogo',//上传文件夹名
      'auto': true, //文件添加到上传队列后,自动进行上传。默认为false
      'multi': false,//是否允许多文件上传。默认为false
      'fileExt':'*.gif;*.jpg;*.jpeg;*.png',//允许上传的文件类型,使用分号(”;)”分割 例如:*.jpg;*.gif,默认为null(所有文件类型)
      'fileDesc':'不超过2M的图片 (*.gif;*.jpg;*.png)',
      'sizeLimit': 2048000, //允许上传的文件大小(kb) 此为2M
      'onSelectOnce': function(event,data) { //在单文件或多文件上传时,选择文件时触发
        //event 事件对象(the event object)
        //data 选择的操作信息
        //data.filesSelected 选择文件操作中选中的文件数量
        $('#status-message').text(data.filesSelected + ' 文件正在等待上传…….'); 
      },
      'onComplete': function(event, queueID, fileObj, response, data) {//当单个文件上传完成后触发
        //event:事件对象(the event object)
        //ID:该文件在文件队列中的唯一表示
        //fileObj:选中文件的对象,他包含的属性列表
        //response:服务器端返回的Response文本,我这里返回的是处理过的文件名称
        //data:文件队列详细信息和文件上传的一般数据

        alert("文件:" + fileObj.name + " 上传成功!");
        //设置图片名称
        $("#applogo").attr("value",response);
        //设置输入框的值
        $("#text_webApplogo").attr("value",fileObj.name);
        //设置图片路径
        $("#img_64").attr("src","Imagelogo/64_"+response);
        $("#img_48").attr("src","Imagelogo/48_"+response);
        $("#img_32").attr("src","Imagelogo/32_"+response);
        $("#img_16").attr("src","Imagelogo/16_"+response);
        //图片路径设置完成后,显示图片
        $("#webApplogo_tab").css("display","block");
      },
      'onError': function(event, queueID, fileObj) {//当单个文件上传出错时触发
        alert("文件:" + fileObj.name + " 上传失败!");
      },
      'buttonImg':'Images/bn_04.gif',//浏览按钮的图片路径
      'width':60,//浏览按钮的宽和高
      'height':24
      ,'queueID':'QID_webApplogo'//页面上作为文件上传队列的元素的ID
    });

3、服务器端处理文件上传

/// <summary>
  /// 上传文件
  /// </summary>
  public class UploadApplogo : IHttpHandler
  {
    System.Drawing.Image image, image64, image48, image32, image16; //定义image类的对象
    protected string imagePath;//图片路径
    protected string imageType;//图片类型
    protected string imageName;//图片名称
    protected string fileName;//图片名称
    //提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
    //如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
    System.Drawing.Image.GetThumbnailImageAbort callb = null;

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      HttpPostedFile UploadImage = context.Request.Files["FileData"];
      //物理路径
      string uploadpath = HttpContext.Current.Server.MapPath(context.Request["folder"] + "\");

      if (UploadImage != null)
      {
        //是否有目录,如没有就创建
        if (!Directory.Exists(uploadpath))
        {
          Directory.CreateDirectory(uploadpath);
        }
        //客户端文件完全名称
        string filename = UploadImage.FileName;
        string extname = filename.Substring(filename.LastIndexOf(".") + 1);
        //为不重复,设置文件名
        fileName = Guid.NewGuid().ToString() + "." + extname;

        //context.Response.Write("1");
        context.Response.Write(fileName);
      }
      else
      {
        context.Response.Write("0");
      }

      string mPath;      

      imagePath = UploadImage.FileName;
      //取得图片类型
      imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
      //取得图片名称
      imageName = imagePath.Substring(imagePath.LastIndexOf("\") + 1);
      Stream imgStream = UploadImage.InputStream;//流文件,准备读取上载文件的内容
      int imgLen = UploadImage.ContentLength;//上载文件大小

      //建立虚拟路径
      mPath = HttpContext.Current.Server.MapPath(context.Request["folder"]);
      //保存到虚拟路径
      UploadImage.SaveAs(mPath + "\" + fileName);
      ////显示原图
      //imageSource.ImageUrl = "upFile/" + imageName;
      //为上传的图片建立引用
      image = System.Drawing.Image.FromFile(mPath + "\" + fileName);

      //生成缩略图
      image64 = image.GetThumbnailImage(64, 64, callb, new System.IntPtr());
      //把缩略图保存到指定的虚拟路径
      image64.Save(HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\64_" + fileName);
      //释放image64对象的资源
      image64.Dispose();

      //生成缩略图
      image48 = image.GetThumbnailImage(48, 48, callb, new System.IntPtr());
      image48.Save(HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\48_" + fileName);
      image48.Dispose();

      //生成缩略图
      image32 = image.GetThumbnailImage(32, 32, callb, new System.IntPtr());
      image32.Save(HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\32_" + fileName);
      image32.Dispose();

      //生成缩略图
      image16 = image.GetThumbnailImage(16, 16, callb, new System.IntPtr());
      image16.Save(HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\16_" + fileName);
      image16.Dispose();

      //释放image对象占用的资源
      image.Dispose();
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }

4、效果如下

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

jquery

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

2026.10.22

60

11

Gradle Wrapper配置教程
Gradle Wrapper配置教程

本专题介绍Gradle Wrapper的基本作用,涵盖文件组成、生成方法、版本配置和任务调用,帮助你理解项目如何使用指定的Gradle版本,减少不同开发环境带来的构建差异。

2026.10.22

40

10

Golangci-lint检查器使用教程
Golangci-lint检查器使用教程

本专题整理Golangci-lint常用Linters的用途与配置方法,覆盖错误处理、无效代码、安全、复杂度、命名和依赖检查,帮助项目合理选规则。

2026.10.22

40

15

Gradle怎么构建Java项目
Gradle怎么构建Java项目

本专题介绍Gradle Java项目构建方法,涵盖Java插件配置、依赖声明、源码编译、测试执行和打包检查,帮助你理解各阶段的任务关系,逐步完成项目构建。

2026.10.22

20

10

PHP时间戳漏洞修复教程
PHP时间戳漏洞修复教程

本专题汇总PHP常见时间戳漏洞,包括2038年溢出、时区漂移、时间戳伪造、重放攻击、时间盲注与日志审计失效等,并给出升级64位环境、使用 DateTimeImmutable、统一UTC、严格超时校验、预处理绑定、hash_equals 比对等修复方案,帮助开发者快速排查和加固时间相关代码。

2026.10.22

20

25

Golangci-lint配置文件与规则设置
Golangci-lint配置文件与规则设置

本专题聚焦Golangci-lint YAML配置文件,讲清运行参数、规则启停、目录排除、问题过滤及严重级别设置,便于团队统一检查标准。

2026.10.22

20

15

Golangci-lint安装与入门教程
Golangci-lint安装与入门教程

本专题讲解Golangci-lint的安装、初始化、基础命令和首次代码检查,覆盖本地环境、Docker及多平台用法,帮助Go开发者快速上手。

2026.10.22

20

15

PHP2Shell高危漏洞修复教程
PHP2Shell高危漏洞修复教程

PHP2Shell漏洞是什么,哪些PHP版本和运行环境会受到影响,服务器应该如何检测与修复?本专题提供版本自查脚本、Linux与Docker升级命令、PHP-FPM和Apache排查步骤,以及修复验证、临时缓解和应急响应方案。

2026.10.22

160

12

剧云人物设定与关系管理教程
剧云人物设定与关系管理教程

本专题介绍剧云人物设定与关系管理,讲解角色小传、性格职业、人物目标、背景资料和关系网络的创建及维护方法。

2026.10.22

20

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
jQuery官方API文档
jQuery官方API文档

共0课时 | 0人学习

jQuery 教程
jQuery 教程

共42课时 | 16.5万人学习

HTML+CSS基础与实战
HTML+CSS基础与实战

共132课时 | 19.4万人学习