在网页中显示asci文件的内容

发布于 2024-11-08 04:35:33 字数 4392 浏览 0 评论 0原文

在我的应用程序中,我想显示一些由用户(管理员)上传的静态文件(.html、.htm、.txt),然后将它们放在指定的目录中。

另外,管理员可以向目录添加新的文件夹或文件,所以我认为使用 asp:treeview 显示文件列表是一个好主意,我发现了这个:

http://mattberseth.com/blog/2007/07/hwo_to_create_an_aspnet_ajax_s.html

左侧的树视图是我想要的,即使它只是读取文件夹并在树中列出,所以我做了一些修复来列出文件夹和文件,我还制作了树可以编辑:

规则.aspx

<form id="form" runat="server">
    <div>
                <table id="tbl" cellpadding="0px" cellspacing="0px">            
                    <tr>
                        <td style="border:solid 1px black" valign="top">
                            <div style="overflow:auto;width:300px;height:450px;">
                                <asp:TreeView 
                                    ID="tvFolders" runat="server" 
                                    OnSelectedNodeChanged="TvFolders_SelectedNodeChanged">
                                    <NodeStyle 
                                        ImageUrl="Img/folder.gif" HorizontalPadding="3px" 
                                        Font-Underline="false" ForeColor="black" />
                                    <SelectedNodeStyle 
                                        Font-Underline="true" Font-Bold="true" />
                                </asp:TreeView> 
                            </div>
                        </td>
                    </tr>
                </table>         
        <br /> 
    </div>
    <!-- The tree editor controls -->
    <div id="addFold" runat="server"></div>
    <div id="addFile" runat="server"></div>
    <div id="deleteFile" runat="server"></div>
    <div id="deleteFold" runat="server"></div>


    <!-- Div used to show the content of the file -->
    <div id="contentDiv" runat="server"></div>
</form>

规则.aspx.cs:

private DbService db=new DbService();
private bool isAdmin;
protected void Page_Load(object sender, EventArgs e)
{
    isAdmin=db.isUserAdmin(Context.Identify.user.name);

    if (!this.IsPostBack)
    {
        string rootFolder = this.Server.MapPath("files/");

        TreeNode rootNode = new TreeNode("Root", rootFolder);
        rootNode.Expanded = true;
        rootNode.Select();
        this.tvFolders.Nodes.Add(rootNode);

        BindDirs(rootFolder, rootNode);

        //set the editor button display or not according the type of current user
        setEditorVisibility();
    }
}

private void setEditorVisibility(){
    //if user select the directory,and he is the admin,so he can add fold/file under this directory,or delete this fold.
    addFold.visibile=deleteFold.visibile=addFile.visibile=isAdmin && Directory.Exist(ootNode.selectedNode.value);

    // if user select the file,and he is the admin,he can delte/update it.
    deleteFile.visibe=isAdmin && File.Exist(ootNode.selectedNode.value);
}
protected void TvFolders_SelectedNodeChanged(object sender, EventArgs args)
{
    setEditorVisibility();

    //now show the content in the contentDiv of the page
    if(File.Exist(ootNode.selectedNode.value)){
        this.contentDiv.innerHtml=xxxx? 
        //here how to make the content of the file displayed in the div?
        //I am sure the type of the file will be .html .htm or .txt.
    }
}

private static void BindDirs(string path, TreeNode treeNode)
{
    if (!string.IsNullOrEmpty(path))
    {
        foreach (string directoryPath in System.IO.Directory.GetDirectories(path))
        {
            System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(directoryPath);
            TreeNode subNode = new TreeNode(directory.Name, directory.FullName);
            treeNode.ChildNodes.Add(subNode);

            // bind sub directories
            BindDirs(directoryPath, subNode);
        }

        //add the file in the tree list
        foreach (string filePath in File.getFiles(path))
        {
            FileInfo info = new FileInfo(filePath);
            TreeNode subNode = new TreeNode(info.Name, info.FullName);
            treeNode.ChildNodes.Add(subNode);
        }
    }
}   

现在我只是不知道当用户选择绑定到文件的节点时如何显示文件内容。

有什么建议吗?

In my application,I want to display some static files(.html,.htm,.txt) which will be uploaded by the user(the admin),then I put them in a specified directory.

Also,the admin can add new folder or files to the directory,so I think using the asp:treeview to display the file list is a good idea,and I found this :

http://mattberseth.com/blog/2007/07/hwo_to_create_an_aspnet_ajax_s.html

enter image description here

The tree view in the left is what I want even it just read the folders and list the in the tree,so I made some fix to list both folders and files,also I make the tree can be edited:

The rule.aspx

<form id="form" runat="server">
    <div>
                <table id="tbl" cellpadding="0px" cellspacing="0px">            
                    <tr>
                        <td style="border:solid 1px black" valign="top">
                            <div style="overflow:auto;width:300px;height:450px;">
                                <asp:TreeView 
                                    ID="tvFolders" runat="server" 
                                    OnSelectedNodeChanged="TvFolders_SelectedNodeChanged">
                                    <NodeStyle 
                                        ImageUrl="Img/folder.gif" HorizontalPadding="3px" 
                                        Font-Underline="false" ForeColor="black" />
                                    <SelectedNodeStyle 
                                        Font-Underline="true" Font-Bold="true" />
                                </asp:TreeView> 
                            </div>
                        </td>
                    </tr>
                </table>         
        <br /> 
    </div>
    <!-- The tree editor controls -->
    <div id="addFold" runat="server"></div>
    <div id="addFile" runat="server"></div>
    <div id="deleteFile" runat="server"></div>
    <div id="deleteFold" runat="server"></div>


    <!-- Div used to show the content of the file -->
    <div id="contentDiv" runat="server"></div>
</form>

The rule.aspx.cs:

private DbService db=new DbService();
private bool isAdmin;
protected void Page_Load(object sender, EventArgs e)
{
    isAdmin=db.isUserAdmin(Context.Identify.user.name);

    if (!this.IsPostBack)
    {
        string rootFolder = this.Server.MapPath("files/");

        TreeNode rootNode = new TreeNode("Root", rootFolder);
        rootNode.Expanded = true;
        rootNode.Select();
        this.tvFolders.Nodes.Add(rootNode);

        BindDirs(rootFolder, rootNode);

        //set the editor button display or not according the type of current user
        setEditorVisibility();
    }
}

private void setEditorVisibility(){
    //if user select the directory,and he is the admin,so he can add fold/file under this directory,or delete this fold.
    addFold.visibile=deleteFold.visibile=addFile.visibile=isAdmin && Directory.Exist(ootNode.selectedNode.value);

    // if user select the file,and he is the admin,he can delte/update it.
    deleteFile.visibe=isAdmin && File.Exist(ootNode.selectedNode.value);
}
protected void TvFolders_SelectedNodeChanged(object sender, EventArgs args)
{
    setEditorVisibility();

    //now show the content in the contentDiv of the page
    if(File.Exist(ootNode.selectedNode.value)){
        this.contentDiv.innerHtml=xxxx? 
        //here how to make the content of the file displayed in the div?
        //I am sure the type of the file will be .html .htm or .txt.
    }
}

private static void BindDirs(string path, TreeNode treeNode)
{
    if (!string.IsNullOrEmpty(path))
    {
        foreach (string directoryPath in System.IO.Directory.GetDirectories(path))
        {
            System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(directoryPath);
            TreeNode subNode = new TreeNode(directory.Name, directory.FullName);
            treeNode.ChildNodes.Add(subNode);

            // bind sub directories
            BindDirs(directoryPath, subNode);
        }

        //add the file in the tree list
        foreach (string filePath in File.getFiles(path))
        {
            FileInfo info = new FileInfo(filePath);
            TreeNode subNode = new TreeNode(info.Name, info.FullName);
            treeNode.ChildNodes.Add(subNode);
        }
    }
}   

Now I just do not know how to display the file content when user select a node which is binded to a file.

ANy suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2024-11-15 04:35:33

怎么样:

this.contentDiv.innerHtml=File.ReadAllText(ootNode.selectedNode.value);

编辑
如果是文本文件,上面的代码将返回正常的行结尾而不是 html 行结尾,因此您可以对文本文件执行以下操作:

this.contentDiv.innerHtml=File.ReadAllText(ootNode.selectedNode.value).Replace("\r\n","</br>").Replace("\r","</br>").Replace("\n","</br>");

显然仍然需要异常处理

How About:

this.contentDiv.innerHtml=File.ReadAllText(ootNode.selectedNode.value);

EDIT
The above will bring back normal line endings instead of html line endings if it's a text file, so you can do below for text files:

this.contentDiv.innerHtml=File.ReadAllText(ootNode.selectedNode.value).Replace("\r\n","</br>").Replace("\r","</br>").Replace("\n","</br>");

Obviously still needs exception handling

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文