设为首页 收藏本站
查看: 1165|回复: 0

[经验分享] SharePoint 2010 列表项代码绑定附件心得 (FileUpload上传附件)

[复制链接]

尚未签到

发表于 2015-9-29 11:45:48 | 显示全部楼层 |阅读模式
  最近项目中用到在插入Item时绑定附件,可以上传多个附件,很快就写出来了,可是测试一侧老是有问题,经过多番折腾,终于算通过测试。SharePoint 2010上传附件需注意一下几点:


  • 判断文件是否为空,即文件内容什么都没有。
  • 判断文件的扩展名是否存在。
  • 判断文件名称是否包含特殊字符,参考http://support.microsoft.com/kb/894629
  • 判断文件扩展名称是否被禁用,在管理中心可设置。
  • 判断文件上传大小,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;
}

  
    

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-120414-1-1.html 上篇帖子: 修改计算机名导致sharepoint 2010站点打不开的解决办法 下篇帖子: sharepoint 里面 查询列表操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表