车皮 发表于 2015-9-26 00:08:04

SharePoint 上传附件

  更改SharePoint自定义列表的新建页面,开发自定义控件,通过SPListItem newItem = List.Items.Add(), newItem["Title"]="Title"; newItem.Update(),进行新建项目操作。
  若需要上传附件,需要通过操作SPAttachmentCollection对象:

Code
SPAttachmentCollection attachments = newItem.Attachments;
                  attachments.Add(item.AttachFileName, item.AttachBytes);
                  newItem.Update();  
  注意: 附件是和SPListItem关联的,必须确认newItem已经保存(newItem.Update());
  或者调用attachmentns.AddNow(AttachFileName,AttachBytes)。
  
  上传附件,将附件转换成字节流存储到数据库中,在自定义控件中可以使用FileUpLoad控件,后台处理直接去FileUpLoad.FileBytes获取文件字节,不要再从其PostedFile.InputStream中获取流,否则文件虽然被上传了,但有可能文件内容空白,虽然大小、页数、字数都在。
  标准的写法:

Code
if (uploadFile.HasFile && uploadFile.PostedFile != null && uploadFile.PostedFile.ContentLength > 0)
            {
                pt.AttachFileName = uploadFile.FileName;
                pt.AttachBytes = uploadFile.FileBytes;
            }  
  
  可能会出错的写法:
  

Code
uploadObj.FileBytes = new byte;
Stream fStream = fileUpload.PostedFile.InputStream;
fStream.Read(uploadObj.FileBytes, 0, fileUpload.PostedFile.ContentLength);
fStream.Close();
fStream.Dispose();
uploadObj.FileBytes = fileUpload.FileBytes;  这种写法在ASPX页面是没问题,但是放在Sharepoint页面下就会出现上面的情况。
  原因我也不太清楚,是否是Sharepoint的HttpModule有过此类的过滤或者其他的条件,有知道的朋友可以给指点下。
页: [1]
查看完整版本: SharePoint 上传附件