0

私のアプリケーションでは、ユーザー(管理者)によってアップロードされる静的ファイル(.html、.htm、.txt)をいくつか表示したいので、それらを指定されたディレクトリに配置します。

また、管理者はディレクトリに新しいフォルダやファイルを追加できるので、asp:treeviewを使用してファイルリストを表示するのは良い考えだと思います。これを見つけました:

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

ここに画像の説明を入力してください

左側のツリービューは、フォルダを読み取ってツリーに一覧表示するだけでも必要なものです。そこで、フォルダとファイルの両方を一覧表示するように修正しました。また、ツリーを編集できるようにします。

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>

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);
        }
    }
}   

これで、ユーザーがファイルにバインドされているノードを選択したときにファイルの内容を表示する方法がわかりません。

なにか提案を?

4

1 に答える 1

0

どうですか:

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>");

明らかに、まだ例外処理が必要です

于 2011-05-20T10:50:34.987 に答える