1    //附件实体类  
 2    class Document
 3         {
 4             public string MingCheng { get; set; }
 5             public string Count { get; set; }
 6 
 7 
 8 
 9 
10 
11             public byte[] Filebyte { get; set; }
12         }
13 
14  
15 
16     //声明附件文件list,构造实体类
17         List<Document> documents = new List<Document>();
18 
19  
20 
21         这个是选择附件的按钮事件 
22 
23         void btnChoose1_Click(object sender, RoutedEventArgs e)
24         {
25             documents.Clear();
26             OpenFileDialog dialog = new OpenFileDialog();
27             //是否允许上传多个文件
28             dialog.Multiselect = true;
29             if (dialog.ShowDialog().Value)
30             {
31                 foreach (FileInfo item in dialog.Files)
32                 {
33                     // 选择上传的文件
34                     //FileInfo file = dialog.File;
35                     Stream stream = item.OpenRead();
36                     stream.Position = 0;
37                     byte[] buffer = new byte[stream.Length];
38                     //将文件读入字节数组
39                     stream.Read(buffer, 0, buffer.Length);
40                     String fileExtention = item.Extension;
41 
42 
43                     Document document = new Document();
44                     document.MingCheng = item.Name;
45                     document.Count = item.Length.ToString();
46                     document.Filebyte = buffer;
47                     ListBoxItem listitem = new ListBoxItem();
48                     listitem.Style = this.Resources["ListBoxItemStyle1"] as Style;
49                     listitem.Content = document.MingCheng;
50                     uploadlist1.Items.Add(listitem);
51                     documents.Add(document);
52                 }
53             }
54             //else
55             //{
56             //    MessageBox.Show("请选择文件!");
57             //}
58         }
59 
60  
61 
62  
63 
64         //这个方法是上传方法
65 
66         /// <summary>
67         /// 提交附件
68         /// </summary>
69         /// <param name="siteURL">网站url</param>
70         /// <param name="documentListName">list名称</param>
71         /// <param name="documentListURL">上传listurl</param>
72         /// <param name="documentName">文件名称</param>
73         /// <param name="documentStream">文件字节流</param>
74 
75         public void UploadDocument(int id, string siteURL, string documentListName, string documentListURL, string documentName, byte[] documentStream)
76         {
77             using (ClientContext clientContext = new ClientContext(siteURL))
78             {
79                 List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
80                 clientContext.Load(documentsList);
81                 var fileCreationInformation = new FileCreationInformation();
82                 //指定内容 byte[]数组,这里是 documentStream 
83                 fileCreationInformation.Content = documentStream;
84                 //允许文档覆盖 
85                 fileCreationInformation.Overwrite = true;
86                 fileCreationInformation.Url = siteURL + documentListURL + id + "/" + documentName;
87                 Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
88                 uploadFile.ListItemAllFields.Update();
89                 clientContext.ExecuteQueryAsync(loadSiteDataa, null);
90 
91 
92             }
93 
94 
95         }
96 
97 
98         public static ClientRequestSucceededEventHandler loadSiteDataa { get; set; }

 

posted on 2012-09-07 09:14    阅读(374)  评论(0编辑  收藏  举报