SharePoint 2010 列表项代码绑定附件心得 (FileUpload上传附件)

本文详细介绍了在SharePoint 2010中进行附件上传时的验证步骤,包括检查文件内容、扩展名、特殊字符、文件类型及大小限制等,并提供了具体的代码实现。

最近项目中用到在插入Item时绑定附件,可以上传多个附件,很快就写出来了,可是测试一侧老是有问题,经过多番折腾,终于算通过测试。SharePoint 2010上传附件需注意一下几点:

  1. 判断文件是否为空,即文件内容什么都没有。
  2. 判断文件的扩展名是否存在。
  3. 判断文件名称是否包含特殊字符,参考http://support.microsoft.com/kb/894629
  4. 判断文件扩展名称是否被禁用,在管理中心可设置。
  5. 判断文件上传大小,SharePoint 2010  默认是50M。

Code: 

 

 

 


  
1        if (FileUpload1.PostedFile.ContentLength == 0 )
2 {
3 string str = @" The file name is invalid or the file is empty. A file name can not contain any of the following characters: \ / : * ? "" < > | # { } % ~ & " ;
4 labMsg.Text = str;
5 labMsg.Visible = true ;
6 return ;
7 }
8 if (FileUpload1.PostedFile != null )
9 {
10 // Check upload file extension name
11   string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
12 if (fileExtension == string .Empty || fileExtension == null )
13 {
14 string str = @" The file is not extension name. " ;
15 labMsg.Text = str;
16 labMsg.Visible = true ;
17 return ;
18 }
19 else
20 {
21 labMsg.Visible = false ;
22 }
23 string fileName = FileUpload1.FileName;
24 int index = fileName.LastIndexOf( " . " );
25 fileName = fileName.Substring( 0 , index);
26 List < string > characters = new List < string > (); // \ / : * ? " < > | # { } % ~ & // 特殊字符不只有这些,
27   char c = ' " ' ;
28 characters.Add( " \\ " );
29 characters.Add( " / " );
30 characters.Add( " : " );
31 characters.Add( " * " );
32 characters.Add( " ? " );
33 characters.Add(c.ToString());
34 characters.Add( " < " );
35 characters.Add( " > " );
36 characters.Add( " | " );
37 characters.Add( " # " );
38 characters.Add( " { " );
39 characters.Add( " } " );
40 characters.Add( " % " );
41 characters.Add( " ~ " );
42 characters.Add( " & " );
43 foreach ( string str in characters)
44 {
45 if (fileName.Contains(str))
46 {
47 string message = @" The file name is invalid or the file is empty. A file name can not contain any of the following characters: \ / : * ? "" < > | # { } % ~ & . " ;
48 labMsg.Text = message;
49 labMsg.Visible = true ;
50 return ;
51 }
52 }
53
54 if (CSP.Infrastructure.CSPUtility.CheckUploadFileType(fileExtension.Substring( 1 )))
55 {
56 string message = @" The following file(s) have been blocked by the administrator: " + FileUpload1.FileName;
57 labMsg.Text = message;
58 labMsg.Visible = true ;
59 return ;
60 }
61
62 if (FileUpload1.PostedFile.ContentLength > 52100000 )
63 {
64 labMsg.Text = " Can not upload file that the file size lager 50 M. " ;
65 labMsg.Visible = true ;
66 return ;
67 }
68 else
69 {
70 labMsg.Visible = false ;
71 }
72
73 if (ViewState[ " Attachment " ] == null )
74 {
75 ViewState[ " Attachment " ] = CreateDtAttachment();
76 }
77 DataTable dt = (DataTable)ViewState[ " Attachment " ];
78 string name = FileUpload1.FileName.Trim();
79 DataRow[] rows = dt.Select( " AttachmentName = ' " + name + " ' " );
80 if (rows.Length > 0 )
81 {
82 // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "<script>alert('" + FileUpload1.FileName + " is exist !');</script>");
83   labMsg.Text = FileUpload1.FileName + " already exists. " ;
84 labMsg.Visible = true ;
85 return ;
86 }
87 else
88 {
89 labMsg.Visible = false ;
90 }
91 DataRow row = dt.NewRow();
92 if (dt.Rows.Count != null )
93 {
94 row[ " AttachmentId " ] = (dt.Rows.Count + 1 ).ToString();
95 }
96 else
97 {
98 row[ " AttachmentId " ] = " 0 " ;
99 }
100
101 row[ " AttachmentName " ] = FileUpload1.FileName;
102
103 int len = FileUpload1.FileBytes.Length;
104 byte [] fileContent = new byte [len];
105 FileUpload1.FileContent.Read(fileContent, 0 , len);
106
107 row[ " AttachContent " ] = fileContent;
108 dt.Rows.Add(row);
109 ViewState[ " Attachment " ] = dt;
110 GvDataBind();
111 }

 

处理上传文件的特殊符号是比较繁琐的一件事情.SharePoint自带上传文件报错为:"The file name is invalid or the file is empty. A file name can not contain any of the following characters: \ / : * ? "" < > | # { } % ~ & ";

其实还有一些其他特殊字符也会报出此错误。
 
在上传文件的时候需要取得管理中心,限制上传文件的类型,这样就可以与管理中心允许上传文件的列表保持一致,代码如下:
 public static bool CheckUploadFileType(string typeName)
        {
            bool isExtensonFile = false;
            SPSite site = SPContext.Current.Site;            
            SPWebApplication webapp = site.WebApplication;
            if (webapp.BlockedFileExtensions.Contains(typeName))
            {
                isExtensonFile = true;
            }
            return isExtensonFile;
        }

 

 

转载于:https://www.cnblogs.com/dexter2003/archive/2010/12/05/1887806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值