JQuery上传插件Uploadify的使用

本文介绍如何利用Uploadify插件实现文件无刷新上传功能。文中提供了详细配置步骤及示例代码,并演示了如何设置文件类型、大小限制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在开发 web程序的时候,上传附件或是照片这是常有的事件,以前一直在使用swfupload,后来发现了jQuery的Uploadify感觉不错,就试了一下,这里做一个记录。以备使用。

       Uploadify的下载地址:http://www.uploadify.com/download/

       这里注意一点,因为这种无刷新上传全是通过flash实现的,所以我们还要去下载一个swfobject.js

       swfobject.js的下载地址:https://github.com/swfobject/swfobject

       当然了,还要它的基础类jquery.min.js文件,这里我用的是jquery-1.7.1.min.js这个网上很容易找,而且我想大家手里也都有就不多说了。

       Uploadify的官方使用说明:http://www.uploadify.com/documentation/ 这个英文不好也能看明白,他都给出了例子,还是很不错的。

       中文的说明:http://wenku.baidu.com/link?url=ZMRK05I8PO8plcCOejmrvFOb2au-TItby9oLqnO2olc-A9GZuLrPtJ70EOp4pCIj8XBHnGk6-8nhYAhWlcakyUxtKhzfMp1l0WlHpk6xz9O

      下面我们来看一下程序的例子:

       1,先下载Uploadify的压缩包,然后解压,把里面的文件解压到你的项目的js文件夹中。其中有用的文件就是四个。

             jquery.uploadify.js(jquery.uploadify.min.js),uploadify.css,uploadify.swf,uploadify-cancel.png

       2,将swfobject.js,jquery-1.7.1.min.js放到js文件夹中

       3,新建Default.html文件,代码如下:

     

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  5.     <title></title>  
  6.     <link href="js/uploadify.css" rel="stylesheet" />  
  7.     <script src="js/jquery-1.7.1.min.js"></script>  
  8.     <script src="js/swfobject.js"></script>  
  9.     <script src="js/jquery.uploadify.min.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(function () {  
  12.             $("#file_upload").uploadify({  
  13.                 //开启调试    
  14.                 'debug': false,  
  15.                 //是否自动上传    
  16.                 'auto': false,  
  17.                 'buttonText': '选择照片',  
  18.                 //flash    
  19.                 'swf': "js/uploadify.swf",  
  20.                 'queueSizeLimit': 5,  
  21.                 //文件选择后的容器ID    
  22.                 'queueID': 'uploadfileQueue',  
  23.                 //后台运行上传的程序  
  24.                 'uploader': 'upload.ashx',  
  25.                 'width': '75',  
  26.                 'height': '24',  
  27.                 //是否支持多文件上传,这里为true,你在选择文件的时候,就可以选择多个文件  
  28.                 'multi': true,  
  29.                 'fileTypeDesc': '支持的格式:',  
  30.                 'fileTypeExts': '*.jpg;*.jpge;*.gif;*.png',  
  31.                 'fileSizeLimit': '1MB',  
  32.                 //上传完成后多久删除进度条  
  33.                 'removeTimeout': 1,  
  34.   
  35.                 //返回一个错误,选择文件的时候触发    
  36.                 'onSelectError': function (file, errorCode, errorMsg) {  
  37.                     switch (errorCode) {  
  38.                         case -100:  
  39.                             alert("上传的文件数量已经超出系统限制的" + $('#file_upload').uploadify('settings', 'queueSizeLimit') + "个文件!");  
  40.                             break;  
  41.                         case -110:  
  42.                             alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#file_upload').uploadify('settings', 'fileSizeLimit') + "大小!");  
  43.                             break;  
  44.                         case -120:  
  45.                             alert("文件 [" + file.name + "] 大小异常!");  
  46.                             break;  
  47.                         case -130:  
  48.                             alert("文件 [" + file.name + "] 类型不正确!");  
  49.                             break;  
  50.                     }  
  51.                 },  
  52.                 //检测FLASH失败调用    
  53.                 'onFallback': function () {  
  54.                     alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");  
  55.                 },  
  56.                 //上传到服务器,服务器返回相应信息到data里    
  57.                 'onUploadSuccess': function (file, data, response) {  
  58.                     alert(file.name);  
  59.                     $("#mypic").attr("src", data);  
  60.                 },  
  61.                 //多文件上传,服务器返回相应的信息  
  62.                 'onQueueComplete': function (queueData) {  
  63.                     alert(queueData.uploadsSuccessful);  
  64.                 }  
  65.             });  
  66.         });  
  67.   
  68.         //开始上传  
  69.         function doUpload() {  
  70.             $('#file_upload').uploadify('upload', '*');  
  71.         }  
  72.         //停止上传  
  73.         function closeLoad() {  
  74.             $('#file_upload').uploadify('cancel', '*');  
  75.         }  
  76.     </script>  
  77. </head>  
  78. <body>  
  79.     <table width="704" border="0" align="center" cellpadding="0" cellspacing="0" id="__01">    
  80.         <tr>    
  81.             <td align="center" valign="middle">    
  82.                 <div id="uploadfileQueue" style="padding: 3px;">    
  83.                 </div>    
  84.                 <div id="file_upload">    
  85.                 </div>    
  86.             </td>    
  87.         </tr>    
  88.         <tr>    
  89.             <td height="50" align="center" valign="middle">    
  90.                 <input type="button" value="上传" onclick="doUpload()" />  
  91.                 <input type="button" value="取消" onclick="closeLoad()" />   
  92.             </td>    
  93.         </tr>    
  94.         <tr>  
  95.             <td>  
  96.                 <img id="mypic" width="100" height="120" />  
  97.             </td>  
  98.         </tr>  
  99.     </table>    
  100. </body>  
  101. </html>  

     4,编写后台代码:upload.ashx

     

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace JQueryUploadDemo  
  8. {  
  9.     /// <summary>  
  10.     /// upload 的摘要说明  
  11.     /// </summary>  
  12.     public class upload : IHttpHandler  
  13.     {  
  14.   
  15.         public void ProcessRequest(HttpContext context)  
  16.         {  
  17.             context.Response.ContentType = "text/plain";  
  18.             context.Response.Charset = "utf-8";   
  19.   
  20.             HttpPostedFile file = context.Request.Files["Filedata"];  
  21.             string uploadPath = context.Server.MapPath("\\UploadFile\\");  
  22.             string filename = DateTime.Now.ToString("yyyyMMddhhmmssffff") + ".jpg";  
  23.             if (file != null)  
  24.             {  
  25.                 if (!Directory.Exists(uploadPath))  
  26.                 {  
  27.                     Directory.CreateDirectory(uploadPath);  
  28.                 }  
  29.                   
  30.                 file.SaveAs(uploadPath + filename);  
  31.                 //生成缩略图    
  32.                 //MakeThumbnail(uploadPath + file.FileName, uploadPath + "\\s\\" + file.FileName, 80, 80);  
  33.             }  
  34.   
  35.             context.Response.Clear();  
  36.             context.Response.Write("/UploadFile/" + filename);  
  37.             context.Response.End();  
  38.         }  
  39.   
  40.         private void MakeThumbnail(string sourcePath, string newPath, int width, int height)  
  41.         {  
  42.             System.Drawing.Image ig = System.Drawing.Image.FromFile(sourcePath);  
  43.             int towidth = width;  
  44.             int toheight = height;  
  45.             int x = 0;  
  46.             int y = 0;  
  47.             int ow = ig.Width;  
  48.             int oh = ig.Height;  
  49.             if ((double)ig.Width / (double)ig.Height > (double)towidth / (double)toheight)  
  50.             {  
  51.                 oh = ig.Height;  
  52.                 ow = ig.Height * towidth / toheight;  
  53.                 y = 0;  
  54.                 x = (ig.Width - ow) / 2;  
  55.   
  56.             }  
  57.             else  
  58.             {  
  59.                 ow = ig.Width;  
  60.                 oh = ig.Width * height / towidth;  
  61.                 x = 0;  
  62.                 y = (ig.Height - oh) / 2;  
  63.             }  
  64.             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);  
  65.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);  
  66.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;  
  67.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  68.             g.Clear(System.Drawing.Color.Transparent);  
  69.             g.DrawImage(ig, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);  
  70.             try  
  71.             {  
  72.                 bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);  
  73.             }  
  74.             catch (Exception ex)  
  75.             {  
  76.                 throw ex;  
  77.             }  
  78.             finally  
  79.             {  
  80.                 ig.Dispose();  
  81.                 bitmap.Dispose();  
  82.                 g.Dispose();  
  83.             }  
  84.   
  85.         }    
  86.   
  87.         public bool IsReusable  
  88.         {  
  89.             get  
  90.             {  
  91.                 return false;  
  92.             }  
  93.         }  
  94.     }  
  95. }  

    OK,是不是很简单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值