前几天,由于项目的需求,需要在infopath中上传多附件,当我用重复表设计好表单后。以为,客户的需求已经很容易的就搞定了,没想到当我在测试的到时候。发现了infopath在一次上传附件时,最大容量才5M,没办法,只好想别的解决方案了。之后,想到的方案就是:在表单提交之前讲infopath表单中的附件分解并上传到MOSS文档库中。
这样不当可以解决附件上传的问题,同时也避免了表单容量大的问题,可谓是一举两得,同时也支持附件的删除,首先看看设计的表单,两个重复表,一个是用来上传多附件,另一个是用来存放上传附件的列表,当看到,上传附件列表中已经有数据是,可以到相应的文档库中找到infopath中上传的附件,就是这样了,下面来看看终点的,代码部分
代码部分主要的技术点就是infopath附件的分解和对infopath重复表的操作命名空间的引用
usingMicrosoft.Office.InfoPath;
usingSystem;
usingSystem.Xml;
usingSystem.Xml.XPath;
usingSystem.Windows.Forms;
usingSystem.Text;
首先是注册两个按钮事件
publicvoidInternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["CTRL12_5"]).Clicked+=newClickedEventHandler(CTRL12_5_Clicked);
((ButtonEvent)EventManager.ControlEvents["CTRL18_5"]).Clicked+=newClickedEventHandler(CTRL18_5_Clicked);
}
编写上传按钮事件,主要处理的是文件的分解,上传,以及重复表的删除,增加
publicvoidCTRL12_5_Clicked(objectsender,ClickedEventArgse)
{
//在此处编写代码。 XPathNavigatordomNav=MainDataSource.CreateNavigator();
XPathNodeIteratorrows=domNav.Select("/my:myFields/my:group3/my:group5",NamespaceManager);
XPathNodeIteratorrows1;
stringmyNamespace=NamespaceManager.LookupNamespace("my");
while(rows.MoveNext())
{
rows1=domNav.Select("/my:myFields/my:group6/my:group7",NamespaceManager);
stringattachment=rows.Current.SelectSingleNode("my:attachment",NamespaceManager).Value;
byte[]attachmentBytes=Convert.FromBase64String(attachment);
intnamebufferlen=attachmentBytes[20]*2;
byte[]fileNameBuffer=newbyte[namebufferlen];
//Theactualfilenamestartsatposition24
for(inti=0;i<namebufferlen;i++)
{
fileNameBuffer[i]=attachmentBytes[24+i];
}
//originalfilenameminusthelastcharacter!
char[]asciiChars=UnicodeEncoding.Unicode.GetChars(fileNameBuffer);
stringfileName=newstring(asciiChars);
fileName=fileName.Substring(0,fileName.Length-1);
//arrayName.Add(fileName);
//Thefileislocatedaftertheheader,whichis24byteslong
//plusthelengthofthefilename.
byte[]fileContent=newbyte[attachmentBytes.Length-(24+namebufferlen)];
for(inti=0;i<fileContent.Length;i++)
{
fileContent[i]=attachmentBytes[24+namebufferlen+i];
}
APWS.AgilePartsapws=new附件.APWS.AgileParts();
stringsavePath=@"http://ascentn-moss:8080/Shared%20Documents";
apws.Credentials=System.Net.CredentialCache.DefaultCredentials;
stringattachUrl=apws.UploadFileToSPS2(savePath+"/"+fileName,true,fileContent);
intk=0;
if(rows1.Count==0)
k=1;
else
k=rows1.Count+1;
using(XmlWriterwriter=MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group6",NamespaceManager).AppendChild())
{
writer.WriteStartElement("group7",myNamespace);
writer.WriteElementString("fieldID",myNamespace,k.ToString());
writer.WriteElementString("fieldURL",myNamespace,attachUrl);
writer.WriteElementString("fieldNAME",myNamespace,fileName);
writer.WriteEndElement();
writer.Close();
}
}
intgroup2NodesCount=rows.Count;
XPathNavigatorfirstGroup2NodeNav=domNav.SelectSingleNode("/my:myFields/my:group3/my:group5[1]",
NamespaceManager);
XPathNavigatorlastGroup2NodeNav=domNav.SelectSingleNode("/my:myFields/my:group3/my:group5["+group2NodesCount.ToString()+"]",NamespaceManager);
firstGroup2NodeNav.DeleteRange(lastGroup2NodeNav);
}
删除事件,主要处理了对文档库文件的删除以及infopath表单重复表行的删除,都是比较基本的操作
publicvoidCTRL18_5_Clicked(objectsender,ClickedEventArgse)
{
//在此处编写代码。
stringstrfileUrl=e.Source.SelectSingleNode("/my:myFields/my:group6/my:group7/my:fieldURL",NamespaceManager).Value;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
using(SPSitemySite=newSPSite("http://ascentn-moss:8080"))
{
mySite.AllowUnsafeUpdates=true;
SPWebwebs=mySite.OpenWeb();
webs.AllowUnsafeUpdates=true;
webs.GetFile(strfileUrl).Delete();
}
}
catch(Exceptionex)
{
throwex;
}
});
e.Source.DeleteSelf();
}