locked
Create links to pdf files RRS feed

  • Question

  • User-1936142408 posted

    I hope this post is in the correct forum. We have a folder on the server that has pdf files. The file names are in the format of 'report_name_propname_04_21_12.pdf'. The report_name is different as is the prop_name and 04 is the month, 21 is the day of month, and the 12 is the year. The goal is to have links on the web page that has the prop_name(s). When clicked it would display a list of months as links. ie 01,02,03,...11,12 under the propname link. When these month links are clicked the web page would display a list of links of the pdf files. The part of displaying the pdf link I have. The question I have is how can this be achived in code? Is there a control for this functionality? I think it is like an Excel pivot table.

    Does anyone have a suggestion?

    Thank you in advance,

    Jim

    Wednesday, May 23, 2012 5:07 PM

Answers

  • User1954304945 posted

    Hi, you can create a DataTable with four columns (“Year”, “Month”, “Day”, “FilePath”) to store the file information. With the methods in DirectoryInfo, FileInfo, you can get the “Year”, “Month”, “Day”, “FilePath” information from the files. Then, store the information into DataTable, bind “Year” column to a DropDownList, bind “Month” column to another DropDownList. I made an example and tested on my computer, you can refer to it:

    Default.aspx:

    <!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></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" 
                AppendDataBoundItems="true" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>Please select Year...</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" 
                onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList3" runat="server">
            </asp:DropDownList>
        </div>
        </form>
    </body>
    </html>
    

    Default.aspx.cs:

    public DataTable table = new DataTable();
            protected void Page_Load(object sender, EventArgs e)
            {
                table = GetFiles();
                if (!this.IsPostBack)
                {
                    List<string> Year = GetYears();
                    this.DropDownList1.DataSource = Year;
                    this.DropDownList1.DataBind();
                }
            }
    
            protected DataTable GetFiles()
            {
                DataTable table = new DataTable();
                DataColumn column = new DataColumn("Year", System.Type.GetType("System.String"));
                table.Columns.Add(column);
                column = new DataColumn("Month", System.Type.GetType("System.String"));
                table.Columns.Add(column);
                column = new DataColumn("Day", System.Type.GetType("System.String"));
                table.Columns.Add(column);
                column = new DataColumn("FilePath", System.Type.GetType("System.String"));
                table.Columns.Add(column);
    
                string Year = "";
                string Month = "";
                string Day = "";
                string FilePath = "";
                DirectoryInfo direc = new DirectoryInfo("File Directory");
                FileInfo[] FileNames = direc.GetFiles();
                foreach (FileInfo file in FileNames)
                {
                    string FileName = file.FullName;
                    string FileEx = FileName.Substring(FileName.LastIndexOf("."));
    
                    if (FileEx == ".pdf")
                    {
                        Year = FileName.Substring(FileName.LastIndexOf(".") - 2, 2);
                        Day = FileName.Substring(FileName.LastIndexOf(".") - 5, 2);
                        Month = FileName.Substring(FileName.LastIndexOf(".") - 8, 2);
                        FilePath = file.FullName;
    
                        DataRow row = table.NewRow();
                        row["Year"] = Year;
                        row["Day"] = Day;
                        row["Month"] = Month;
                        row["FilePath"] = FilePath;
    
                        table.Rows.Add(row);
                    }
                }
    
                return table;
            }
    
            protected List<string> GetYears()
            {
                List<string> list = new List<string>();
    
                list = (from myRow in table.AsEnumerable()
                        select myRow.Field<string>("Year")).Distinct<string>().ToList<string>();
                return list;
            }
    
            protected List<string> GetMonths(string year)
            {
                List<string> list = new List<string>();
                if (year != null)
                {
                    list = (from myRow in table.AsEnumerable()
                            where myRow.Field<string>("Year") == year
                            select myRow.Field<string>("Month")).Distinct<string>().ToList<string>();
                }
    
                return list;
            }
    
    
            protected List<string> GetFilePathes(string year,string month)
            {
                List<string> list = new List<string>();
                if (year != null && month != null)
                {
                    list = (from myRow in table.AsEnumerable()
                            where myRow.Field<string>("Year") == year && myRow.Field<string>("Month") == month
                            select myRow.Field<string>("FilePath")).Distinct<string>().ToList<string>();
                }
    
                return list;
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string Year = this.DropDownList1.SelectedValue;
                List<string> Month = GetMonths(Year);
                this.DropDownList2.Items.Clear();
                this.DropDownList2.DataSource = Month;
                this.DropDownList2.DataBind();
                ListItem item = new ListItem();
                item.Value = null;
                item.Text = "Please select a month...";
                DropDownList2.Items.Add(item);
                item.Selected = true;
            }
    
            protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
            {
                string Year = this.DropDownList1.SelectedValue;
                string Month = this.DropDownList2.SelectedValue;
                List<string> FilePathes = GetFilePathes(Year, Month);
                this.DropDownList3.DataSource = FilePathes;
                this.DropDownList3.DataBind();
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 25, 2012 5:42 AM

All replies

  • User197322208 posted

    iterate in the directory finding files

    put into a LIst<>

    bind to a grid.

    Wednesday, May 23, 2012 10:13 PM
  • User1954304945 posted

    Hi, you can create a DataTable with four columns (“Year”, “Month”, “Day”, “FilePath”) to store the file information. With the methods in DirectoryInfo, FileInfo, you can get the “Year”, “Month”, “Day”, “FilePath” information from the files. Then, store the information into DataTable, bind “Year” column to a DropDownList, bind “Month” column to another DropDownList. I made an example and tested on my computer, you can refer to it:

    Default.aspx:

    <!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></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" 
                AppendDataBoundItems="true" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>Please select Year...</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" 
                onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList3" runat="server">
            </asp:DropDownList>
        </div>
        </form>
    </body>
    </html>
    

    Default.aspx.cs:

    public DataTable table = new DataTable();
            protected void Page_Load(object sender, EventArgs e)
            {
                table = GetFiles();
                if (!this.IsPostBack)
                {
                    List<string> Year = GetYears();
                    this.DropDownList1.DataSource = Year;
                    this.DropDownList1.DataBind();
                }
            }
    
            protected DataTable GetFiles()
            {
                DataTable table = new DataTable();
                DataColumn column = new DataColumn("Year", System.Type.GetType("System.String"));
                table.Columns.Add(column);
                column = new DataColumn("Month", System.Type.GetType("System.String"));
                table.Columns.Add(column);
                column = new DataColumn("Day", System.Type.GetType("System.String"));
                table.Columns.Add(column);
                column = new DataColumn("FilePath", System.Type.GetType("System.String"));
                table.Columns.Add(column);
    
                string Year = "";
                string Month = "";
                string Day = "";
                string FilePath = "";
                DirectoryInfo direc = new DirectoryInfo("File Directory");
                FileInfo[] FileNames = direc.GetFiles();
                foreach (FileInfo file in FileNames)
                {
                    string FileName = file.FullName;
                    string FileEx = FileName.Substring(FileName.LastIndexOf("."));
    
                    if (FileEx == ".pdf")
                    {
                        Year = FileName.Substring(FileName.LastIndexOf(".") - 2, 2);
                        Day = FileName.Substring(FileName.LastIndexOf(".") - 5, 2);
                        Month = FileName.Substring(FileName.LastIndexOf(".") - 8, 2);
                        FilePath = file.FullName;
    
                        DataRow row = table.NewRow();
                        row["Year"] = Year;
                        row["Day"] = Day;
                        row["Month"] = Month;
                        row["FilePath"] = FilePath;
    
                        table.Rows.Add(row);
                    }
                }
    
                return table;
            }
    
            protected List<string> GetYears()
            {
                List<string> list = new List<string>();
    
                list = (from myRow in table.AsEnumerable()
                        select myRow.Field<string>("Year")).Distinct<string>().ToList<string>();
                return list;
            }
    
            protected List<string> GetMonths(string year)
            {
                List<string> list = new List<string>();
                if (year != null)
                {
                    list = (from myRow in table.AsEnumerable()
                            where myRow.Field<string>("Year") == year
                            select myRow.Field<string>("Month")).Distinct<string>().ToList<string>();
                }
    
                return list;
            }
    
    
            protected List<string> GetFilePathes(string year,string month)
            {
                List<string> list = new List<string>();
                if (year != null && month != null)
                {
                    list = (from myRow in table.AsEnumerable()
                            where myRow.Field<string>("Year") == year && myRow.Field<string>("Month") == month
                            select myRow.Field<string>("FilePath")).Distinct<string>().ToList<string>();
                }
    
                return list;
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string Year = this.DropDownList1.SelectedValue;
                List<string> Month = GetMonths(Year);
                this.DropDownList2.Items.Clear();
                this.DropDownList2.DataSource = Month;
                this.DropDownList2.DataBind();
                ListItem item = new ListItem();
                item.Value = null;
                item.Text = "Please select a month...";
                DropDownList2.Items.Add(item);
                item.Selected = true;
            }
    
            protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
            {
                string Year = this.DropDownList1.SelectedValue;
                string Month = this.DropDownList2.SelectedValue;
                List<string> FilePathes = GetFilePathes(Year, Month);
                this.DropDownList3.DataSource = FilePathes;
                this.DropDownList3.DataBind();
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 25, 2012 5:42 AM
  • User-1936142408 posted

    Hi Allen,

    Thank you for your response. Now that I have a datatable, how can I loop thru it and select “filepath” where year, month and day are a certain value?

    Thanks in advance,

    Jim

    Thursday, May 31, 2012 5:14 PM
  • User-1936142408 posted

    Hi Allen,

    how do I mark your reply as the answer?

    Thanks,

    Jim

    Friday, June 1, 2012 11:54 AM