.NET easyui+ajaxfileupload图片上传

easyui框架+ajaxfileupload.js+ashx一般处理程序实现

前台:


    
    
    
    
    

图片预览:
图片标题:   

后台:upload.ashx

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //图片名 
            HttpFileCollection files = context.Request.Files;
            string msg = string.Empty;
            string error = string.Empty;
            string imgurl;

            string filepath = "~/linkimgs";     //文件路径          
            
            //判断服务器目录是否存在
            if (System.IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹
            {
                System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(filepath));
               
            }

            if (files.Count > 0)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    if (file.ContentLength > 0)
                    {
                        //全路径 
                        string FullFullName = file.FileName;
                        //获取图片的名称 
                        String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);

                        string path = "~/linkimgs";

                        file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + fileName);

                        msg = "上传成功! 文件大小为:" + convertFileSize(files[0].ContentLength);
                        imgurl = "../linkimgs/" + file.FileName;
                        string res = "{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + imgurl + "'}";
                        context.Response.Write(res);
                        context.Response.End();
                    }
                }
            }
        }

        public static String convertFileSize(long size)
        {
            long kb = 1024;
            long mb = kb * 1024;
            long gb = mb * 1024;
            if (size >= gb)
            {
                return string.Format("{0:F1} GB", (float)size / gb); 
            }
            else if (size >= mb)
            {
                float f = (float)size / mb;
                return string.Format(f > 100 ? "{0:F1} MB" : "{0:F1} MB", f);                 
            }
            else if (size >= kb)
            {
                float f = (float)size / kb;
                return string.Format(f > 100 ? "{0:F1} KB" : "{0:F1} KB", f); 
            }
            else
                return string.Format("{0:F1} B", size); 
        }

 

你可能感兴趣的:(技术支持,ASP.NET,HTML,JavaScript,jquery,ajax)