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

最近项目中用到在插入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;
        }

 

 

posted on 2010-12-05 14:29  张大明  阅读(1301)  评论(0编辑  收藏  举报

导航