InfoPath multiattachment upload Solution

Source: Internet
Author: User

A few days ago, due to project requirements, multiple attachments need to be uploaded in infopath. When I design a form with a repeated table. I thought it was easy for the customer to meet their needs.

I didn't expect it to happen when I was testing it. Infopath has a maximum capacity of 5 MB when uploading an attachment.

No, so I had to think about other solutions. Then, we thought of the solution: to break down the attachments in the infopath form and upload them to the MOSS document library before the form is submitted.

Improper uploading can solve the problem of attachment uploading and avoid the problem of large form capacity.

You can also delete attachments.

 

First, let's take a look at the design form, two duplicate tables. One is used to upload multiple attachments, and the other is used to store the list of uploaded attachments.

When you see that the uploaded attachments list contains data, you can find the attachments uploaded in infopath in the corresponding document library. The attachments marked with "new" are decomposed by the attachments in infopath.

This is the case. Let's take a look at the final part of the code.

The main technical points in the Code section are the decomposition of infopath attachments and the operations on infopath repeated tables.

Namespace reference

 

Using Microsoft. Office. InfoPath;
Using System;
Using System. Xml;
Using System. Xml. XPath;
Using System. Windows. Forms;
Using System. Text;

 

First, register two button events

 

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 );
}

 

The upload button event is mainly used to process the file decomposition, upload, and deletion of duplicate tables.

 

Public void CTRL12_5_Clicked (object sender, ClickedEventArgs e)
{
// Write code here.

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 attachment. APWS. AgileParts ();
String savePath = @ "http: // ascentn-moss: 8080/Shared % 20 Documents ";
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 );

}

 

Deletion events mainly process the deletion of document library files and the deletion of duplicate table rows in infopath forms, which are basic operations.

Public void CTRL18_5_Clicked (object sender, ClickedEventArgs e)
{
// Write code here.
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 ();
}

 

This is the basic thing here. We will not mention infopath form design here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.