主题: 请问有谁能把file_manager_json.php翻译成aspx吗?(已自己用手解决)
作者: edui, 发布日期: 2010-01-18 21:18:13, 浏览数: 8577
RT,想要这个功能,但不会PHP语言,有谁能翻译一下吗?
作者: goad, 发布日期: 2010-01-19 13:32:33
这个貌似有点难度,哈哈,我尝试了一下把它转成ASP的都没有成功,最后放弃了,文件管理器还是按自己的习惯写比较合理
回复
作者: edui, 发布日期: 2010-01-19 14:14:12
我已经自己写完了,谢谢
按照作者PHP的JSON格式输出就行了
回复
作者: Roddy, 发布日期: 2010-01-19 15:38:54
恭喜,拿出来共享一下,呵呵
回复
作者: edui, 发布日期: 2010-01-20 15:10:47

很喜欢kindeditor,感谢作者,现分享一下我改写的file_manager_json.aspx,添加了“按时间排序”,参数:date

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

using Common;
namespace Web.editor.aspx
{
    public partial class file_manager_json : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //根目录URL
            string root_url = "/Upload/Editor/";
            //根目录路径
            string root_path = Server.MapPath(root_url);
            if (!root_path.EndsWith("\\")) root_path = root_path + "\\";
           
            //图片扩展名
            string[] ext_arr = new string[] { "gif", "jpg", "jpeg", "png", "bmp" };

            //根据path参数,设置各路径和URL
            string current_path="";
            string current_url="";
            string current_dir_path="";
            string moveup_dir_path="";

            string quePath = PubFun.GetQueryString("path");
            if (quePath == "")
            {
                current_path = root_path;
                current_url = root_url;
                current_dir_path = "";
                moveup_dir_path = "";
            }
            else
            {
                //current_path = root_path + quePath;
                current_url = root_url + quePath;
                current_path = Server.MapPath(current_url);
                if (!current_path.EndsWith("\\")) current_path = root_path + "\\";

                current_dir_path = quePath;
                moveup_dir_path = Regex.Replace(current_dir_path, @"(.*?)[^\/]+\/$", "$1");
            }
            //排序形式,name or size or type
            string queOrder = PubFun.GetQueryString("order").ToLower();

            //不允许使用..移动到上一级目录
            if(Regex.IsMatch(current_path,@"\.\."))
            {
                Response.Write("Access is not allowed.");
                return;
            }
            //最后一个字符不是/
            if(current_path.EndsWith("/"))
            {
                Response.Write("Parameter is not valid.");
                return;
            }
            //目录不存在或不是目录
            if (!Directory.Exists(current_path))
            {
                Response.Write("Directory does not exist.");
                return;
            }
           
            //遍历目录取得文件信息
            string[] dir_list = Directory.GetDirectories(current_path);
            string[] file_list=Directory.GetFiles(current_path);
            switch (queOrder)
            {
                case "name":
                    Array.Sort(dir_list, new NameSorter());
                    Array.Sort(file_list, new NameSorter());
                    break;
                case "size":
                    Array.Sort(dir_list, new NameSorter());
                    Array.Sort(file_list, new SizeSorter());
                    break;
                case "type":
                    Array.Sort(dir_list, new NameSorter());
                    Array.Sort(file_list, new TypeSorter());
                    break;
                case "date":
                default:
                    Array.Sort(dir_list, new DateSorter());
                    Array.Sort(file_list, new DateSorter());
                    break;
            }

            //组合JSON字符串
            string json = "{";
            json += "\"moveup_dir_path\":\"" + moveup_dir_path + "\",";
            json += "\"current_dir_path\":\"" + current_dir_path + "\",";
            json += "\"current_url\":\"" + current_url + "\",";
            json += "\"total_count\":" + (dir_list.Length+file_list.Length) + ",";
            json += "\"file_list\":";
            json += "[";
            string strList = "";
            for (int i = 0; i < dir_list.Length; i++)
            {
                DirectoryInfo dir = new DirectoryInfo(dir_list[i]);
                strList += ",";
                strList += "{";
                strList += "\"is_dir\":true,";//是否文件夹
                strList += "\"has_file\":" + (dir.GetFileSystemInfos().Length > 0 ? "true" : "false") + ",";//文件夹是否包含文件
                strList += "\"filesize\":0,";//文件大小
                strList += "\"is_photo\":false,";//是否图片
                strList += "\"filetype\":\"\",";//文件类别,用扩展名判断
                strList += "\"filename\":\"" + dir.FullName.Substring(dir.FullName.LastIndexOf("\\") + 1) + "\",";//文件名,包含扩展名
                strList += "\"datetime\":\"" + dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") + "\"";//文件最后修改时间
                strList += "}";
            }
            for (int i = 0; i < file_list.Length; i++)
            {
                FileInfo file = new FileInfo(file_list[i]);
                if(Array.IndexOf(ext_arr, file.Extension.Substring(1).ToLower()) == -1 ) continue;
                strList += ",";
                strList += "{";
                strList += "\"is_dir\":false,";//是否文件夹
                strList += "\"has_file\":false,";//文件夹是否包含文件
                strList += "\"filesize\":" + file.Length + ",";//文件大小
                strList += "\"dir_path\":\"\",";
                strList += "\"is_photo\":true,";//是否图片
                strList += "\"filetype\":\"" + file.Extension.Substring(1) + "\",";//文件类别,用扩展名判断
                strList += "\"filename\":\"" + file.Name + "\",";//文件名,包含扩展名
                strList += "\"datetime\":\"" + file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") + "\"";//文件最后修改时间
                strList += "}";
            }
            if (strList != "") strList = strList.Substring(1);
            json += strList;
            json += "]";
            json += "}";
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(json);
        }
    }

    #region 排序
    /// <summary>
    /// 按名称排序
    /// </summary>
    public class NameSorter : IComparer
    {
        public int Compare(object x, object y)
        {
            if (x == null && y == null)
            {
                return 0;
            }
            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }
            FileInfo xInfo =new FileInfo(x.ToString());
            FileInfo yInfo = new FileInfo(y.ToString());

            return xInfo.FullName.CompareTo(yInfo.FullName);//递增  
            //return yInfo.FullName.CompareTo(xInfo.FullName);//递减
        }
    }
    /// <summary>
    /// 按文件大小排序
    /// </summary>
    public class SizeSorter : IComparer
    {
        public int Compare(object x, object y)
        {
            if (x == null && y == null)
            {
                return 0;
            }
            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }
            FileInfo xInfo =new FileInfo(x.ToString());
            FileInfo yInfo = new FileInfo(y.ToString());

            //return xInfo.Length.CompareTo(yInfo.Length);//递增  
            return yInfo.Length.CompareTo(xInfo.Length);//递减  
        }
    }
    /// <summary>
    /// 按文件类型排序
    /// </summary>
    public class TypeSorter : IComparer
    {
        public int Compare(object x, object y)
        {
            if (x == null && y == null)
            {
                return 0;
            }
            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }
            FileInfo xInfo =new FileInfo(x.ToString());
            FileInfo yInfo = new FileInfo(y.ToString());

            return xInfo.Extension.CompareTo(yInfo.Extension);//递增  
            //return yInfo.Extension.CompareTo(xInfo.Extension);//递减  
        }
    }
    /// <summary>
    /// 按修改时间排序
    /// </summary>
    public class DateSorter : IComparer
    {
        public int Compare(object x, object y)
        {
            if (x == null && y == null)
            {
                return 0;
            }
            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }
            FileInfo xInfo =new FileInfo(x.ToString());
            FileInfo yInfo = new FileInfo(y.ToString());

            //return xInfo.LastWriteTime.CompareTo(yInfo.LastWriteTime);//递增  
            return yInfo.LastWriteTime.CompareTo(xInfo.LastWriteTime);//递减  
        }
    }
    #endregion 排序
}
回复
作者: Roddy, 发布日期: 2010-01-20 15:45:01
谢谢分享,我要把这个主题移动到【经验分享】。
回复
作者: xuwei, 发布日期: 2010-02-24 17:16:05
edui:
报错了,有的东西你没有给上来,麻烦你把其它的补上吧,谢谢
回复
作者: 爵罗先生, 发布日期: 2010-05-23 07:15:37
  我是看到楼主的那句——“已自己用手解决”来滴!哈哈
回复
作者: edui, 发布日期: 2010-05-26 15:59:22

to xuwei(虚伪):

里面只用了using Common;我这个公用类,只用到了取url参数的一个函数PubFun.GetQueryString,你可以自己改为Request.QueryString["path"]

回复
作者: edui, 发布日期: 2010-05-26 16:01:44
我当时改这个用的是3.4版本,更新版不一定有效
回复
发表新帖 发表回复