asp..net2.0文档上传 的一个方法(一个上传控件,上传多个文档 )

控件|上传|上传控件

 前台 :
   <table style="width:100%">
         <tr>
             <td style="width:15%">
                 类型名称:</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" style="width: 370px" type="file" /> 
                
                 <asp:Button ID="addBt" runat="server" Text="上 传" /></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="保 存" />
                    <asp:Button ID="returnBt" runat="server" Text="返 回" />
                <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(@"\\", @"\"))
                                    {
                                        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

 

时间: 2024-09-12 15:51:33

asp..net2.0文档上传 的一个方法(一个上传控件,上传多个文档 )的相关文章

asp.net2.0实现主从数据表的简单方法

asp.net|数据 实现效果:在主表里选中某行,从表里得出该行的详情. 方法1:代码实现.在页面上放一个GridView,一个DetailView.数据绑定GridView并且要设置主键,然后在SelectedIndexChanged事件写代码:选择发生变化时,DetailView也改变为相应的Detail.具体代码:using System;using System.Data;using System.Configuration;using System.Collections;using

打开 Office XP 或 Office 2003 文档时,系统提示您授予权限的 ActiveX 控件

故障现象: 当一个"适用于"一节中列出的 Microsoft Office 程序中打开文档时,您可能会提示您授予权限的 ActiveX 控件加载或允许这些控件维护的数据保持不变.某些 ActiveX 控件根本不加载.   此外,导入到另一个程序 (如 Microsoft SharePoint 工作组服务网站包含 ActiveX 控件的 Office 文档时,您可能会收到与以下内容类似的警告消息:   此应用程序时可能不安全的 ActiveX 控件初始化.如果您信任此文档的来源,请选择是

怎么往toolbar控件上动态添加toolbutton?

问题描述 怎么往toolbar控件上动态添加toolbutton? 怎么往toolbar控件上动态添加toolbutton?新增加的toolbutton怎么添加事件处理,怎么响应单击事件呢? 解决方案 http://wenku.baidu.com/link?url=SumPG3rT7WOQaTUiISnyrml0WMXTW0NTmoPh8bhabq10x73-qtMF3E0saL3ZY90wdchQKip3mh1iiAKnkLbSI13j8Y2U2P35beyTCx_wRfu 解决方案二: 在T

ASP.NET2.0:Ilungasoft.Framework.Web之基于Callback的无刷新上传进度条控件[带源码]

asp.net|web|控件|上传|刷新|无刷新 共享一个基于Callback的无刷新上传进度条控件的源码.本控件使用的HttpMoudule基于宝玉的一个上传进度条的sample,这里封装为一个控件,方便使用.无需任何代码,只需设置web.config,添加HttpModule的引用,再将控件拖到页面就行.页面中的文件保存操作和传统的asp.net文件上传完全一样.可以设置属性上传过程中出错或上传成功时跳转到其它页面.兼容IE,Firefox,Opera.其它环境没测试,不过因为是基于Asp.

应用程序-ASP.NET2.0 IIS6 上传文件大小限制

问题描述 ASP.NET2.0 IIS6 上传文件大小限制 已经设置了应用程序下的Web.config中的maxRequestLength大小和metaBase.xml中ASP...Allow的大小了,完全无动于衷! 解决方案 修改web.config中httpRuntime增加maxRequestLength配置就行了(单位kb),metabase.xml是asp的..确认你没改错.. 解决方案二: 解决方案三: 如果设置httpruntime maxRequestLength 还需要在设置你

ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性                                         深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第三篇 ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件 ASP.NET自定义控件组件开发 第三章 为控件添加事件 前

ASP.NET2.0中实现图像转换过滤效果

asp.net|转换 本文描述一种创建定制Web控件的容易方法,并把该控件使用于一个ASP.NET 2.0 web页面中显示微软DirectX图像转换过滤效果. 一.简介 本文描述一种创建定制web控件并应用于一个ASP.NET 2.0 web页面中显示微软DirectX图像转换过滤效果的容易的方法.文中包括一个拥有11个不同控件的类库,每一个控件分别展示微软DirectX图像转换过滤效果的某些方面.在这11个控件中,5个是页面过渡效果控件,其它的6个是用于增强文本外观的过滤效果控件. 在这6个

Asp.Net2.0无刷新客户端回调

asp.net|客户端|刷新|无刷新 Asp.Net2.0的客户端回调是一种很让人激动的方法,他能够让我们控制要提交什么数据给服务器而不用提交整个页面,同时服务器也只返回你所需要的数据而不要发回整个页面. 首先我们要说一个很重要的方法:GetCallbackEventRefernce.我把我的理解写出来,可能是错误的,恳请指出,非常感谢! GetCallbackEventReference 首先实现让客户端脚本有能力传递参数给服务器端的RaiseCallbackEvent方法,然后返回Raise

ASP.NET2.0实现无刷新客户端回调

asp.net|客户端|刷新|无刷新 Asp.Net2.0的客户端回调是一种很让人激动的方法,他能够让我们控制要提交什么数据给服务器而不用提交整个页面,同时服务器也只返回你所需要的数据而不要发回整个页面. 首先我们要说一个很重要的方法:GetCallbackEventRefernce.我把我的理解写出来,可能是错误的,恳请指出,非常感谢! GetCallbackEventReference首先实现让客户端脚本有能力传递参数给服务器端的RaiseCallbackEvent方法,然后返回RaiseC