0

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?

hguser
  • 35,079
  • 54
  • 159
  • 293

1 Answers1

0

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

TBohnen.jnr
  • 5,117
  • 1
  • 19
  • 26
  • This is a choice,but is there other way dot not need read the file directly? here I use a div element to wrap the content,how about use other contorls? iframe? xx.aspx? – hguser May 20 '11 at 11:11
  • I see, so you don't want to display the actual text of the file, but the actual web page contained in the html file? – TBohnen.jnr May 20 '11 at 11:23
  • maybe,for example,if the file is xx.txt,so there is not difference betwen display its actual text or wrap the texts in a web page,but for the html file,it is better to render it as the web page. You can think my page like a frame,each file in the left tree is a link,it have the attribute "href=xxx,target="the contentDIv". – hguser May 20 '11 at 11:56
  • I see, well, you can look at an example like this: http://www.codingforums.com/archive/index.php/t-60619.html but that is a whole lot of effort for doing the same thing, If I may ask, what is your objection to my answer, given that at the end of the day the browser will render both exactly the same way? – TBohnen.jnr May 20 '11 at 12:07
  • Maybe this is caused by my poor English,I do not have any objection to your answer,in fact I am not exactly sure your meaning "display the actual text of the file, but the actual web page contained in the html file". – hguser May 20 '11 at 12:24
  • I think we're just talking past each other :-). Ok, I think my answer should fit the bill, have you tried it? it will display text if it's text and it will display the html if it's html. Can't honestly think of an easier way to do it – TBohnen.jnr May 20 '11 at 12:48
  • Thanks,I will have a try tomorrow, I am at home now. :) – hguser May 20 '11 at 13:15
  • BTW,since the admin can delete a file,so how about if a user requst a file which is being deleted by the admin? – hguser May 20 '11 at 13:16
  • that is where you would have to do exception handling, i.e. if the file exists and worst case scenario do a try catch statement and check for IOException. However the chance of a user requesting the file the exact same time as an admin is deleting it is very very small given that a file delete operation is very fast, especially on files of this type that won't be very large – TBohnen.jnr May 20 '11 at 13:28
  • :) Thanks,I will try tommorrow. You are a kind guy. :_) – hguser May 20 '11 at 13:30
  • @TBohnen.jnr:the manner you provided works,but it will cause some format problem,so I use the iframe. :) Thank you anyway. – hguser May 21 '11 at 12:18