前几天,由于项目的需求,需要在infopath中上传多附件,当我用重复表设计好表单后。以为,客户的需求已经很容易的就搞定了
,没想到当我在测试的到时候。发现了infopath在一次上传附件时,最大容量才5M
没办法,只好想别的解决方案了。之后,想到的方案就是:在表单提交之前讲infopath表单中的附件分解并上传到MOSS文档库中。
这样不当可以解决附件上传的问题,同时也避免了表单容量大的问题,可谓是一举两得
同时也支持附件的删除
首先看看设计的表单,两个重复表,一个是用来上传多附件,另一个是用来存放上传附件的列表
当看到,上传附件列表中已经有数据是,可以到相应的文档库中找到infopath中上传的附件,如图标有“新”字样的就是infopath中的附件分解出来的
效果图,就是这样了,下面来看看终点的,代码部分
代码部分主要的技术点就是infopath附件的分解和对infopath重复表的操作
命名空间的引用
using
Microsoft.Office.InfoPath;
using
System;
using
System.Xml;
using
System.Xml.XPath;
using
System.Windows.Forms;
using
System.Text;
首先是注册两个按钮事件
public
void
InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents[
"
CTRL12_5
"
]).Clicked
+=
new
ClickedEventHandler(CTRL12_5_Clicked);
((ButtonEvent)EventManager.ControlEvents[
"
CTRL18_5
"
]).Clicked
+=
new
ClickedEventHandler(CTRL18_5_Clicked);
}
编写上传按钮事件,主要处理的是文件的分解,上传,以及重复表的删除,增加
public
void
CTRL12_5_Clicked(
object
sender, ClickedEventArgs e)
{
//
在此处编写代码。
XPathNavigator domNav
=
MainDataSource.CreateNavigator();
XPathNodeIterator rows
=
domNav.Select(
"
/my:myFields/my:group3/my:group5
"
, NamespaceManager);
XPathNodeIterator rows1;
string
myNamespace
=
NamespaceManager.LookupNamespace(
"
my
"
);
while
(rows.MoveNext())
{
rows1
=
domNav.Select(
"
/my:myFields/my:group6/my:group7
"
, NamespaceManager);
string
attachment
=
rows.Current.SelectSingleNode(
"
my:attachment
"
, NamespaceManager).Value;
byte
[] attachmentBytes
=
Convert.FromBase64String(attachment);
int
namebufferlen
=
attachmentBytes[
20
]
*
2
;
byte
[] fileNameBuffer
=
new
byte
[namebufferlen];
//
The actual filename starts at position 24
for
(
int
i
=
0
; i
<
namebufferlen; i
++
)
{
fileNameBuffer[i]
=
attachmentBytes[
24
+
i];
}
//
original filename minus the last character !
char
[] asciiChars
=
UnicodeEncoding.Unicode.GetChars(fileNameBuffer);
string
fileName
=
new
string
(asciiChars);
fileName
=
fileName.Substring(
0
, fileName.Length
-
1
);
//
arrayName.Add(fileName);
//
The file is located after the header, which is 24 bytes long
//
plus the length of the filename.
byte
[] fileContent
=
new
byte
[attachmentBytes.Length
-
(
24
+
namebufferlen)];
for
(
int
i
=
0
; i
<
fileContent.Length; i
++
)
{
fileContent[i]
=
attachmentBytes[
24
+
namebufferlen
+
i];
}
APWS.AgileParts apws
=
new
附件.APWS.AgileParts();
string
savePath
=
@"
http://ascentn-moss:8080/Shared%20Documents
"
;
apws.Credentials
=
System.Net.CredentialCache.DefaultCredentials;
string
attachUrl
=
apws.UploadFileToSPS2(savePath
+
"
/
"
+
fileName,
true
, fileContent);
int
k
=
0
;
if
(rows1.Count
==
0
)
k
=
1
;
else
k
=
rows1.Count
+
1
;
using
(XmlWriter writer
=
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();
}
}
int
group2NodesCount
=
rows.Count;
XPathNavigator firstGroup2NodeNav
=
domNav.SelectSingleNode(
"
/my:myFields/my:group3/my:group5[1]
"
,
NamespaceManager);
XPathNavigator lastGroup2NodeNav
=
domNav.SelectSingleNode(
"
/my:myFields/my:group3/my:group5[
"
+
group2NodesCount.ToString()
+
"
]
"
, NamespaceManager);
firstGroup2NodeNav.DeleteRange(lastGroup2NodeNav);
}
删除事件,主要处理了对文档库文件的删除以及infopath表单重复表行的删除,都是比较基本的操作
public
void
CTRL18_5_Clicked(
object
sender, ClickedEventArgs e)
{
//
在此处编写代码。
string
strfileUrl
=
e.Source.SelectSingleNode(
"
/my:myFields/my:group6/my:group7/my:fieldURL
"
, NamespaceManager).Value;
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
try
{
using
(SPSite mySite
=
new
SPSite(
"
http://ascentn-moss:8080
"
))
{
mySite.AllowUnsafeUpdates
=
true
;
SPWeb webs
=
mySite.OpenWeb();
webs.AllowUnsafeUpdates
=
true
;
webs.GetFile(strfileUrl).Delete();
}
}
catch
(Exception ex)
{
throw
ex;
}
});
e.Source.DeleteSelf();
}
到这里基本的东西就是这些了,关于infopath表单设计这里就不提了