1.为列表项上传附件
Stream fStream = FileUpload1.PostedFile.InputStream; byte[] contents = new byte[fStream.Length]; fStream.Read(contents, 0, (int)fStream.Length); fStream.Close(); fStream.Dispose(); FileInfo file = new FileInfo(FileUpload1.PostedFile.FileName); SPListItem item = list.GetItemById(itemId);//要给该记录附加文件 if (item != null) { item.Attachments.Add(file.Name, contents);//给该记录附加文件,可以附加多个附件。 item.Update(); }
2.为文档库上传
site.AllowUnsafeUpdates = true; FileStream fStream = FileUpload1.PostedFile.InputStream; byte[] contents = new byte[fStream.Length]; fStream.Read(contents, 0, (int)fStream.Length); fStream.Close(); site.Files.Add(destUrl, contents, true);// destUrl是文件在服务器上存储的地址,例如http://mysite/document/abc.txt site.AllowUnsafeUpdates = false;