Jquery.Treeview+Jquery UI制作Web文件预览

简介:

效果图:


前台Html:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_26.QingShan.WebFileViewer._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Jquery.Treeview+Jquery UI制作Web文件预览</title>
        <%--JS--%>
        <script src="Scripts/jquery-1.9.1.js" type="text/javascript"> </script>
        <script src="Scripts/jquery.treeview/jquery.cookie.js" type="text/javascript"> </script>
        <script src="Scripts/jquery.treeview/jquery.treeview.js" type="text/javascript"> </script>
        <script src="Scripts/jquery-ui-1.10.3/jquery-ui-1.10.3.min.js" type="text/javascript"> </script>
        <%--CSS--%>
        <link href="Scripts/jquery-ui-1.10.3/css/start/jquery-ui.css" rel="stylesheet" type="text/css" />
        <link href="Scripts/jquery.treeview/jquery.treeview.css" rel="stylesheet"></link>
        <link href="Style/common.css" rel="stylesheet" type="text/css" /> 
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="main">
                <p>
                    文件预览</p>
                <div class="mainContent">
                    <ul id="fileList" class="filetree">
                        <%= FileTreeHtml %></ul>
                    <script type="text/javascript">
                        $(function() {
                            //树形文件目录
                            $(".filetree").treeview();
                            //显示ToolTips
                            $(document).tooltip({
                                items: ".file",
                                track: true,
                                content: function() {
                                    var element = $(this);
                                    var name = element.attr("name");
                                    var img = element.attr("img");
                                    if (img != "") {
                                        return "<img class='toolTips' alt='" + name + "' src='" + img + "'>";
                                    }
                                    return "";
                                }
                            });

                        });
                    </script>
                </div>
            </div>
        </form>
    </body>
</html>
后台代码:

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;

namespace _26.QingShan.WebFileViewer
{
    public partial class _Default : Page
    {
        protected string FileTreeHtml { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var directory = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/FileLibrary"));
                if (Directory.Exists(directory.FullName))
                {
                    FileTreeHtml = FileTreeHelper.GetGuideTree(new StringBuilder(), directory.FullName,
                                                               directory.FullName);
                }
            }
        }
    }
}
生成树形导航Html的类:

using System;
using System.IO;
using System.Text;
using System.Web;

namespace Whir.WebSite.JavascriptDemos
{
    /// <summary>
    ///     树形文件夹Html内容生成类
    /// </summary>
    public class FileTreeHelper
    {
        /// <summary>
        ///     生成树形文件Html
        /// </summary>
        /// <param name="builder">用于存放拼接的Html,由于是递归拼接,调用方法时,传入空的StringBuilder即可</param>
        /// <param name="path">要显示的服务器端文件夹路径(物理路径)</param>
        /// <param name="replacePath">要替换掉的路径部分</param>
        /// <returns></returns>
        public static string GetGuideTree(StringBuilder builder, string path, string replacePath)
        {
            var currentDir = new DirectoryInfo(path);
            DirectoryInfo[] subDirs = currentDir.GetDirectories();
            if (subDirs.Length > 0)
            {
                builder.AppendFormat("<li><span class='folder' path='{0}'>{1}</span>" + Environment.NewLine,
                                     currentDir.FullName.Replace(replacePath, ""), currentDir.Name);
                builder.Append("    <ul>" + Environment.NewLine);
                foreach (DirectoryInfo dir in subDirs)
                {
                    GetGuideTree(builder, dir.FullName, replacePath);
                } 
                FileInfo[] files = currentDir.GetFiles();
                if (files.Length > 0)
                {
                    foreach (FileInfo file in files)
                    {
                        string previewUrl = file.FullName.IsImage()
                                                ? GetFileWebUrl(
                                                    file.FullName.Replace(HttpContext.Current.Server.MapPath("~/"), ""))
                                                : string.Empty;
                        builder.AppendFormat("<li><span class='file' name='{0}' img='{1}' path='{2}'>{0}</span>" + Environment.NewLine, file.Name,
                                             previewUrl, file.FullName.Replace(replacePath, ""));
                    }
                }

                builder.Append("    </ul>" + Environment.NewLine);
                builder.Append("</li>" + Environment.NewLine);
            }
            else
            {
                builder.AppendFormat("<li  class='closed'><span class='folder' path='{0}'>{1}</span>" + Environment.NewLine,
                                     currentDir.FullName.Replace(replacePath, ""), currentDir.Name);
            }
            return builder.ToString();
        }

        public static string GetFileWebUrl(string filePath)
        {
            if (filePath.IsEmpty())
            {
                return string.Empty;
            }
            filePath = filePath.Replace("\\", "/");
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.TrimStart('/');
            }
            return VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Request.ApplicationPath) + filePath;
        }
    }
}


 
完整Demo下载 http://download.csdn.net/detail/a497785609/6926313 



目录
相关文章
|
2月前
|
机器学习/深度学习 人工智能 前端开发
机器学习PAI常见问题之web ui 项目启动后页面打不开如何解决
PAI(平台为智能,Platform for Artificial Intelligence)是阿里云提供的一个全面的人工智能开发平台,旨在为开发者提供机器学习、深度学习等人工智能技术的模型训练、优化和部署服务。以下是PAI平台使用中的一些常见问题及其答案汇总,帮助用户解决在使用过程中遇到的问题。
|
2月前
在 CRM WebClient UI Attachment 区域,创建支持 Web Service 的 Word 文档
在 CRM WebClient UI Attachment 区域,创建支持 Web Service 的 Word 文档
23 0
|
3月前
|
数据可视化 Shell Linux
shell+crontab+gitlab实现ecs服务器文件的web展示
本文通过把ecs服务器上的文件定时上传至gitlab,实现文件的页面可视化和修改历史。技术点:shell、crontab、gitlab。
54 3
|
3月前
|
Web App开发 JSON JavaScript
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
29 0
|
4月前
|
缓存 自然语言处理 物联网
LLama Factory+ModelScope实战——使用 Web UI 进行监督微调
LLaMA Factory 是一个高效的大语言模型训练和推理框架,它通过提供一站式的 Web UI 界面和集成多种训练方法,简化了大模型的微调过程,并能够适配多种开源模型。
|
6天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
25 0
|
9天前
|
JavaScript 前端开发 索引
【Web 前端】jQuery 里的 each() 是什么函数?你是如何使用它的?
【5月更文挑战第2天】【Web 前端】jQuery 里的 each() 是什么函数?你是如何使用它的?
|
9天前
|
JavaScript 前端开发 C++
【Web 前端】JavaScript window.onload 事件和 jQuery ready 函数有何不同?
【5月更文挑战第2天】【Web 前端】JavaScript window.onload 事件和 jQuery ready 函数有何不同?
|
9天前
|
JavaScript 前端开发
【Web 前端】如何在点击一个按钮时使用 jQuery 隐藏一个图片?
【5月更文挑战第2天】【Web 前端】如何在点击一个按钮时使用 jQuery 隐藏一个图片?
|
10天前
|
JavaScript 前端开发
【Web 前端】 jQuery 里的 ID 选择器和 class 选择器有何不同?
【5月更文挑战第1天】【Web 前端】 jQuery 里的 ID 选择器和 class 选择器有何不同?