前臺 :
   <table >
         <tr>
             <td >
                 類型名稱:</td>
             <td>
                <asp:dropdownlist id="typeddl" runat="server" width="100px">
                 </asp:dropdownlist>
                      <asp:label id="lb1" runat="server" text="創建人 :"></asp:label>
                 <asp:label id="writelb" runat="server" text=""></asp:label>
                              
                 <asp:label id="lb2" runat="server" text="創建時間:"></asp:label>
                 <asp:label id="createtimelb" runat="server" text=""></asp:label>
                 </td>
          </tr>
        
          <tr>
              <td>
                  文檔標題:</td>
              <td>
                  <asp:textbox id="titletb" runat="server" width="300px"></asp:textbox></td>
          </tr>
          <tr>
              <td>
                  文檔內容:</td>
              <td>
                  <asp:textbox id="contenttb" runat="server" textmode="multiline" rows="20" columns="80"></asp:textbox></td>
          </tr>
          
         <tr>
             <td>
                 附件上傳:</td>
             <td>
                 <input id="findfile" runat="server" type="file" /> 
                 
                 <asp:button id="addbt" runat="server" text="上 傳" onclick="addbt_click" /></td>   
             </tr>
           <tr>
             <td>
             </td>
             <td>
                <asp:datalist id="viewattachdl" runat="server" repeatdirection="horizontal" backcolor="#cccccc" bordercolor="#999999" borderstyle="solid" borderwidth="3px" cellpadding="4" cellspacing="2" forecolor="black" gridlines="both" onitemcommand="viewattachdl_itemcommand">
                     <itemtemplate>
                         <table>
                             <tr> 
                               <td align="left"><asp:label id="attachviewlb" runat="server" text='<%#eval("filename") %>'></asp:label>
                               </td>
                               
                             </tr>
                             <tr>
                                <td align="center">
                                     <asp:imagebutton id="attachimgbt" commandname="attachimgbt" runat="server" imageurl="~/images/icon-delete.gif" commandargument='<%#eval("fileid") %>'/>
                                 </td>
                             </tr>
                         </table>
                     </itemtemplate>
                    <footerstyle backcolor="#cccccc" />
                    <selecteditemstyle backcolor="#000099" font-bold="true" forecolor="white" />
                    <itemstyle backcolor="white" />
                    <headerstyle backcolor="black" font-bold="true" forecolor="white" />
                 </asp:datalist>
                  <asp:label id="labdetial" runat="server" text=""></asp:label>
                 <asp:label id="document_attachstatuslb" runat="server" text=""></asp:label>
                 <asp:label id="filelb" runat="server" text=""></asp:label></td>
          </tr>
          <tr>
             <td>
             </td>
             <td>
                
                 <asp:button id="savebt" runat="server" text="保 存" onclick="savebt_click" />
                    <asp:button id="returnbt" runat="server" text="返 回" onclick="returnbt_click" />
                <asp:label id="labstate" runat="server" text=""></asp:label></td>
          </tr>
           <tr>
             <td>
             </td>
             <td>
                </td>
          </tr>
        </table>
后臺  :
using system;
using system.io;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
    public partial class document_add : system.web.ui.page
    {
        sqldataaccess sqlda = new sqldataaccess();
        datatable dt = new datatable();
        function fc = new function();
        string typeid = "-1";
        arraylist files = new arraylist();
        static arraylist hif = new arraylist();
        int filesuploaded = 0;
        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                files.clear();
                if (request.params["typeid"] != null)
                {
                    typeid = request.params["typeid"].tostring();
                    viewstate["typeid"] = typeid;
                    getrequest();
                    bindtypeddl(typeid);
                }
                else
                {
                    this.lb1.visible = false;
                    this.lb2.visible = false;
                    this.writelb.visible = false;
                    this.createtimelb.visible = false;
                    bindtypeddl();
                }
            }
        }
        protected void bindtypeddl()
        {
            string strcmd = "select * from companydocumenttype";
            dt = sqlda.gettable(strcmd);
            this.typeddl.datasource = dt;
            this.typeddl.datavaluefield = "typeid";
            this.typeddl.datatextfield = "typename";
            this.typeddl.databind();
        }
        protected void bindtypeddl(string typeid)
        {
            string strcmd = "select * from companydocumenttype where typeid=" + typeid;
            dt = sqlda.gettable(strcmd);
            this.typeddl.datasource = dt;
            this.typeddl.datavaluefield = "typeid";
            this.typeddl.datatextfield = "typename";
            this.typeddl.databind();
        }
        protected void getrequest()
        {
            if (viewstate["typeid"] != null)
            {
                typeid = viewstate["typeid"].tostring();
            }
            if (typeid != "-1")
            {
                string strtxt = "select * from companydocumenttype inner join xt_user on companydocumenttype.writeid=xt_user.userid where typeid=" + typeid;
                dt = sqlda.gettable(strtxt);
                if (dt.rows.count != 0)
                {
                    this.writelb.text = dt.rows[0]["username"].tostring();
                    this.createtimelb.text = fc.getformattime(dt.rows[0]["createtime"].tostring());
                }
                //string strcmd = "select * from companydocument inner join companydocumenttype on companydocument.typeid=companydocumenttype.typeid inner join xt_user on xt_user.userid=companydocument.writeid where companydocument.typeid=" + typeid;
                //dt = sqlda.gettable(strcmd);
            }
        }
        protected void binddatalist(datatable datalistdt)
        {
            if (datalistdt.rows.count != 0)
            {
     
                this.viewattachdl.datasource = datalistdt;
                this.viewattachdl.databind();
                this.viewattachdl.visible = true;
            }
            else
            {
                this.viewattachdl.visible = false;
            }
        }
        protected void addbt_click(object sender, eventargs e)
        {
            string path = "../company/空類型/", typename = "空類型";
            this.filelb.text = "";
            document_attachstatuslb.text = "";
            this.labdetial.text = "";
           
            if (viewstate["path_type_file"] == null)
            {
                string strcmd = "select * from companydocumenttype where typeid=" + this.typeddl.selectedvalue;
                dt = sqlda.gettable(strcmd);
                if (dt.rows.count != 0)
                {
                    typename = dt.rows[0]["typename"].tostring();
                }
                directoryinfo dirinfo = new directoryinfo(server.mappath("../company/" + typename + "/"));
                path = server.mappath("../company/" + typename + "/");
                viewstate["path"] = path;
                if (!dirinfo.exists)
                {
                    directory.createdirectory(server.mappath("../company/" + typename + "/"));
                }
            }
            //檢查上傳路徑是否已經包含此文件
            bool saveas = true;
            bool fileok = false;
            string fn = system.io.path.getfilename(this.findfile.postedfile.filename);
            string[] str = directory.getfiles(viewstate["path"].tostring());
            string fileexception = system.io.path.getextension(fn).tolower();
            string[] allowedexception ={ ".doc", ".txt" };
            if (str.length != 0)
            {//目錄下有文件時做出比較
                for (int j = 0; j < str.length; j++)
                {
                    if (system.io.path.getfilename(str[j]) == system.io.path.getfilename(findfile.postedfile.filename))
                    {
                        saveas = false;
                    }
                }
                if (saveas)
                {
                    for (int i = 0; i < hif.count; i++)
                    {//檢查數組當中是否已經存在此文件
                        htmlinputfile hif = hif[i] as htmlinputfile;
                        if (system.io.path.getfilename(hif.value) == system.io.path.getfilename(findfile.postedfile.filename))
                        {
                            saveas = false;
                        }
                    }
                    if (saveas)
                    {
                        for (int j = 0; j < allowedexception.length; j++)
                        {
                            if (fileexception == allowedexception[j])
                                fileok = true;
                        }
                        if (fileok)
                        {
                            hif.add(findfile);
                            this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 上傳成功!";
                        }
                        else
                        {
                            this.filelb.text = "請上傳.doc文件,或.txt文件!";
                        }
                    }
                    else
                    {
                        this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + "   文件已經上傳,請不要重復上傳!";
                    }
                }
                else
                {
                    this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + "   文件已經存在,請檢查company下的文件!";
                }
            }
            else
            {//目錄下無文件直接保存!
                for (int i = 0; i < hif.count; i++)
                {//檢查數組當中是否已經存在此文件
                    htmlinputfile hif = hif[i] as htmlinputfile;
                    if (hif.count >= 1)
                    {
                        if (system.io.path.getfilename(hif.value) == system.io.path.getfilename(findfile.postedfile.filename))
                        {
                            saveas = false;
                        }
                    }
                    else
                    {
                        saveas = true;
                    }
                }
                if (saveas)
                {
                    for (int j = 0; j < allowedexception.length; j++)
                    {
                        if (fileexception == allowedexception[j])
                            fileok = true;
                    }
                    if (fileok)
                    {
                        hif.add(findfile);
                        this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + " 上傳成功!";
                    }
                    else
                    {
                        this.filelb.text = "請上傳.doc文件,或.txt文件!";
                    }
                }
                else
                {
                    this.filelb.text = system.io.path.getfilename(findfile.postedfile.filename) + "   文件已經上傳,請不要重復上傳!";
                }
            }
            binddatalist(gettable());
       }
        protected void returnbt_click(object sender, eventargs e)
        {
            response.redirect("~/documentmanage/document_view.aspx?tag=" + this.typeddl.selectedvalue);
        }
        protected datatable gettable()
        {
            datatable dt=new datatable();
            datatable table = new datatable();
            table.columns.add("filename");
            table.columns.add("fileid");
            hif.trimtosize();
            int i = 0;
            foreach(system.web.ui.htmlcontrols.htmlinputfile hif in hif)
            {
                datarow row =table.newrow();
                string fn = system.io.path.getfilename(hif.postedfile.filename);
                row["filename"] = fn;
                row["fileid"] = i++;
                table.rows.add(row);
            }
            dt = table;
            return dt;
        }
        protected void savebt_click(object sender, eventargs e)
        {
           string documentid = "-1";
           if (this.titletb.text != "")
           {
            if (viewstate["path"] != null)
            {//執行代有附件的保存
                string baselocation = viewstate["path"].tostring();
                string status = "";
                
                int document_attachstatus = -1;
                int status = -1;
                this.filelb.text = "";
                document_attachstatuslb.text = "";
                this.labdetial.text = "";
                int i = 0;
             
                    foreach (system.web.ui.htmlcontrols.htmlinputfile hif in hif)
                    {
                        try
                        {
                            i++;
                            string fn = system.io.path.getfilename(hif.postedfile.filename);
                            if (documentid == "-1")
                            {//生成companydocument內的唯一documentid
                                documentid = datetime.now.date.tostring("yyyymmdd") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();
                            }
                            if (i == 1)
                            {//多文件上傳只插入一個documentid
                                if (session["userid"] != null)
                                {
                                    string strcmd1 = "insert into companydocument(documentid,writeid,typeid,tilte,content) values('"
                                              + documentid + "','" + session["userid"].tostring() + "','" + this.typeddl.selectedvalue + "','" + this.titletb.text
                                              + "','" + this.contenttb.text + "')";
                                    status = sqlda.executesql(strcmd1);
                                }
                                else
                                {
                                    response.write("<script>alert('提示:重新登陸!')</script>");
                                }
                            }
                            string txt = "select * from document_attach";
                            dt = sqlda.gettable(txt);
                            if (dt.rows.count != 0)
                            {//document_attach表內有數據進行判斷
                                bool insertattach = true;
                                for (int j = 0; j < dt.rows.count; j++)
                                {
                                    if (dt.rows[j]["url"].tostring() == hif.postedfile.filename.replace(@"http://", @"/"))
                                    {
                                        insertattach = false;
                                    }
                                }
                                if (insertattach)
                                {
                                    string strtxt = "insert into document_attach(documentid,url) values('"
                                                                                  + documentid + "','" + baselocation + fn + "')";
                                    document_attachstatus = sqlda.executesql(strtxt);
                                }
                                else
                                {
                                    document_attachstatuslb.text = "數據庫當中已經存在此文件,請重新命名!";
                                }
                            }
                            else
                            {//document_attach表內無數據,直接插入
                          &nb, sp;     string strtxt = "insert into document_attach(documentid,url) values('"
                                               + documentid + "','" + baselocation + fn + "')";
                                document_attachstatus = sqlda.executesql(strtxt);
                            }
                            if (document_attachstatus > 0)
                            {
                                hif.postedfile.saveas(baselocation + fn);
                                filesuploaded++;
                                status += fn + "<br>";
                                labdetial.text = status + " 文件上傳成功:<br>";
                            }
                        }
                        catch (exception err)
                        {
                            labdetial.text = "錯誤 文件保存 " + baselocation
                                                               + "<br>" + err.tostring();
                        }
                    }
               
             
            }
            else
            {//執行無附件的保存
                if (documentid == "-1")
                {//生成companydocument內的唯一documentid
                    documentid = datetime.now.date.tostring("yyyymmdd") + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();
                }
         
                if (session["userid"] != null)
                {
                    string strcmd1 = "insert into companydocument(documentid,writeid,typeid,tilte,content) values('"
                              + documentid + "','" + session["userid"].tostring() + "','" + this.typeddl.selectedvalue + "','" + this.titletb.text
                              + "','" + this.contenttb.text + "')";
                    int status = sqlda.executesql(strcmd1);
                }
                else
                {
                    response.write("<script>alert('提示:重新登陸!')</script>");
                }
              
            }
              hif.clear();
              this.titletb.text = "";
              this.contenttb.text = "";
              this.labdetial.text = "保存成功!";
              binddatalist(gettable()); 
            }
            else
            {//this.titletb.text == ""
                labdetial.text = "提示:請填寫文檔標題";
                binddatalist(gettable());
                return;
            }
        }
        protected void viewattachdl_itemcommand(object source, datalistcommandeventargs e)
        {
            if (e.commandname == "attachimgbt")
            {
                int fileid =int32.parse(e.commandargument.tostring());
                hif.removeat(fileid);
                binddatalist(gettable());
                this.filelb.text = "";
                document_attachstatuslb.text = "";
                this.labdetial.text = "";
             }
        }
}//end_class
新聞熱點
疑難解答
圖片精選