SharePoint2013和2010 webparth上传附件到其他列表(ViewState实现)

SharePoint2013和2010

webparth上传附件到其他列表

前台:

 <div>
    <asp:FileUpload ID="FileUpload2" runat="server" />
    <asp:Button ID="Button3" runat="server" Text="上传" OnClick="Button1_Click" />
    共上传<asp:Label ID="Label2" runat="server"></asp:Label><asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="审批" />
     <br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>

 

  protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                ViewState["demo"] = 0;
            }
            
        }
        
     
        protected void Button1_Click(object sender, EventArgs e)
        {
           string FileName = FileUpload2.PostedFile.FileName;
           if (FileName == "")
           {
               Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), string.Format("<script>alert('上传文件不能为空!')</script>"));
           }
           else
           {
               string file_KB = FileUpload2.PostedFile.ContentLength.ToString() + "KB<br>";//获取文件大小
               string file = FileUpload2.FileName;//获取上传文件名字
               ViewState["demo"] = Convert.ToInt32(ViewState["demo"]) + 1;//ViewState["demo"]获取个数

               string URL = "http://amid01110/456/" + file + " ";



               string lntext = "<a  href='" + URL + "'  >" + file + "</a>" + "大小" + file_KB + "上传文件成功";



               Label1.Text += Label1.Text.Length > 0 ? "<br />" + lntext : lntext;

               Label2.Text = ViewState["demo"].ToString();
           }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {

            this.UploadFileToDocLib(SPContext.Current.Web, "发送记录", Page.Request["ID"]);//实例化网站,添加的列表名,点开的列表的id
        }
        public void UploadFileToDocLib(SPWeb web, string docLibName, string chname)
        {
            web.AllowUnsafeUpdates = true;//设置允许更改
            SPListItem li = web.Lists["新建"].GetItemById(int.Parse(chname));//点开的列表名和点开的id 
            SPList jl = web.Lists.TryGetList(docLibName);//实例化列表名称
            
            SPListItem it = jl.Items.Add();
            it["标题"] = li["标题"];

            it["附件文件"] = Label1.Text.ToString();
           
            it.Update();
            //li.Delete();
        }

 

posted @ 2014-02-25 17:42  914556495  阅读(215)  评论(0编辑  收藏  举报