Chinaunix首页 | 论坛 | 博客
  • 博客访问: 196772
  • 博文数量: 60
  • 博客积分: 3269
  • 博客等级: 中校
  • 技术积分: 648
  • 用 户 组: 普通用户
  • 注册时间: 2005-09-21 10:48
文章存档

2012年(6)

2011年(6)

2010年(30)

2009年(8)

2007年(6)

2005年(4)

我的朋友

分类: 嵌入式

2012-06-05 17:53:30

 
下载,直接copy到项目里就行。
还有一些应该看看。
 
把下面帖到head里,根据自己的情况改改。

   
   
 
     
     
     
     
       
 
   
   


在页面中使用,主要是script部分,按照其官网说明应放到head里,好像无所谓。
<%
    string filepath = Request.QueryString["dir"];
    if(filepath == null || filepath.Length == 0)
    {
        filepath = "commenfiles";
    }
    string uni_filepath =HttpUtility.UrlEncodeUnicode (filepath);
%>
 
下面是uploadify主体,注意其中的路径不要使用"~"符号,不认
 
以下是页面布局
   


        上传文件——<%=filepath %>
   


    
   
        下面div指示上传列表信息的位置,id="fileQueue"就是前面'queueID'定义的
       
  
        下面input将被uploadify的选择文件按钮替代
             
           
        开始上传|   
        清空上传 
   

   

CommenFileUploadHandler.ashx后台调用在'script'属性定义
 
    public class CommenFileUploadHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            由于存在中文编码问题,曾试图如下设置,没用
            //context.Response.Charset = "GB2312";
            //context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
 
            HttpPostedFile file = context.Request.Files["Filedata"];
            if (file != null)
            {
                // 在有中文的路径时,@context.Request["folder"]会得到乱码,无奈先编码,
                // 在此通过HttpUtility.UrlDecode()解码,保证一下
                string uploadPath = HttpUtility.UrlDecode (@context.Request["folder"]);
                string MapPath =  HttpContext.Current.Server.MapPath(uploadPath );
                if (!System.IO.Directory.Exists(MapPath))
                {
                    System.IO.Directory.CreateDirectory(MapPath);
                }
                // 下面是原始的用法,中文路径乱码
                //string folder = @context.Request["folder"]; // 返回"/filecenter/CommenFiles/XXX"
                string folder = uploadPath.Substring (12); // 返回"CommenFiles/XXX"
                // 获得的中文文件名却不是乱码
                string aliasfn = file.FileName;        // 文件原名
                string extension = System.IO.Path.GetExtension(file.FileName).Substring(1); // 后缀名,".XXX",包括点
                // HttpContext.Current.User.Identity.Name可能会有全局引用麻烦,随时变?
                string uploaderaccount = context.User.Identity.Name;
                string uploadername = "";
                string uploadtime = DateTime.Now.ToString();
                string realfn = DateTime.Now.ToString("yyyyMMddHHmmss") + uploaderaccount + "." + extension; // 新文件名:日期时间账号.后缀
                string type = Get_file_type(extension);
                file.SaveAs(MapPath + "/" + realfn);
 
                //// 保存图片和动画的缩略图
                if (type.CompareTo("image") == 0)
                {
                    //提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
                    //如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
                    System.Drawing.Image.GetThumbnailImageAbort callb = null;
                    System.Drawing.Image image, image_zoom; //定义image类的对象
                    // 创建保存缩略图的文件夹
                    if (!System.IO.Directory.Exists(MapPath + "/imgzoom128_128"))
                        System.IO.Directory.CreateDirectory(MapPath + "/imgzoom128_128");
                    if (!System.IO.Directory.Exists(MapPath + "/imgzoom256_256"))
                        System.IO.Directory.CreateDirectory(MapPath + "/imgzoom256_256");
                    if (!System.IO.Directory.Exists(MapPath + "/imgzoom800_600"))
                        System.IO.Directory.CreateDirectory(MapPath + "/imgzoom800_600");
                    //Stream imgStream = file.InputStream;//流文件,准备读取上载文件的内容
                    //为上传的图片建立引用
                    image = System.Drawing.Image.FromFile(MapPath + "/" + realfn);
                    //生成缩略图
                    image_zoom = image.GetThumbnailImage(128, 128, callb, new System.IntPtr());
                    image_zoom.Save(MapPath + "/imgzoom128_128/" + realfn);
                    image_zoom.Dispose();
                    image_zoom = image.GetThumbnailImage(256, 256, callb, new System.IntPtr());
                    image_zoom.Save(MapPath + "/imgzoom256_256/" + realfn);
                    image_zoom.Dispose();
                    image_zoom = image.GetThumbnailImage(800, 600, callb, new System.IntPtr());
                    image_zoom.Save(MapPath + "/imgzoom800_600/" + realfn);
                    image_zoom.Dispose();
                    //释放image对象占用的资源
                    image.Dispose();
                }
 
                //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失  
                context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
        }
 
        public string Get_file_type(string extension)
        {
            if (String.Compare(extension, "jpg", true) == 0) return "image";
            if (String.Compare(extension, "png", true) == 0) return "image";
            if (String.Compare(extension.ToString(), "avi", true) == 0) return "movie";
            if (extension.Length != 0) return "document";
            return "unknown";
        }
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
 
好像就这些,基本上就行了。有些问题如下:
1、显示不出选择文件按钮:确定设置的那些文件和路径是正确的,尽量绝对路径吧。
2、中文路径乱码:代码中提到解决办法,但传过来的中文文件名却没问题。
3、上传时出现"Http error":问题说不明白,但是我重启动浏览器就好了,可能也和调试时停止服务有关。
 
使用uploadify动态修改参数的方法。但还没用过。
$("#uploadify").uploadifySettings('scriptData',{'name':newName,'age':newAge});
阅读(2462) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~