Sunday, 29 June 2014

Infopath-Upload attachment file name-Upload attachment ot Sharepoint

Solution: Upload attachment file name.
Upload attachment ot Sharepoint.
Get attachment file name.

Add Namespace
using InfoPathAttachmentEncoding;
In buttoon Click .......
XPathNavigator ipFormNav = MainDataSource.CreateNavigator();
XPathNavigator nodeNav = ipFormNav.SelectSingleNode("/my:myFields/my:file", NamespaceManager);
string attachmentValue = string.Empty;
if (nodeNav != null && !String.IsNullOrEmpty(nodeNav.Value))
{
attachmentValue = nodeNav.Value;
// Decode the InfoPath file attachment
InfoPathAttachmentEncoding.InfoPathAttachmentDecoder dec = new InfoPathAttachmentDecoder(attachmentValue);
string fileName = dec.Filename;
XPathNavigator txt = ipFormNav.SelectSingleNode("/my:myFields/my:filename", this.NamespaceManager);
txt.SetValue(fileName);
byte[] data = dec.DecodedAttachment;
// Add the file to a document library
using (SPSite site = new SPSite("http://ServerName"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.Folders["MyDocuments"];
docLib.Files.Add(fileName, data);
web.AllowUnsafeUpdates = false;
web.Close();
}
site.Close();
}
}
------------------------------------------------------
Add new class file to solution file...
InfoPathAttachmentEncoding.cs

Add below code in .cs file
---------------------------------------------------------
using System;
using System.IO;
using System.Text;
namespace InfoPathAttachmentEncoding
{
///
/// Decodes a file attachment and saves it to a specified path.
///
public class InfoPathAttachmentDecoder
{
private const int SP1Header_Size = 20;
private const int FIXED_HEADER = 16;
private int fileSize;
private int attachmentNameLength;
private string attachmentName;
private byte[] decodedAttachment;
///
/// Accepts the Base64 encoded string
/// that is the attachment.
///
public InfoPathAttachmentDecoder(string theBase64EncodedString)
{
byte[] theData = Convert.FromBase64String(theBase64EncodedString);
using (MemoryStream ms = new MemoryStream(theData))
{
BinaryReader theReader = new BinaryReader(ms);
DecodeAttachment(theReader);
}
}
private void DecodeAttachment(BinaryReader theReader)
{
//Position the reader to get the file size.
byte[] headerData = new byte[FIXED_HEADER];
headerData = theReader.ReadBytes(headerData.Length);
fileSize = (int)theReader.ReadUInt32();
attachmentNameLength = (int)theReader.ReadUInt32() * 2;
byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
//InfoPath uses UTF8 encoding.
Encoding enc = Encoding.Unicode;
attachmentName = enc.GetString(fileNameBytes, 0, attachmentNameLength - 2);
decodedAttachment = theReader.ReadBytes(fileSize);
}
public void SaveAttachment(string saveLocation)
{
string fullFileName = saveLocation;
if (!fullFileName.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
fullFileName += Path.DirectorySeparatorChar;
}
fullFileName += attachmentName;
if (File.Exists(fullFileName))
File.Delete(fullFileName);
FileStream fs = new FileStream(fullFileName, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(decodedAttachment);
bw.Close();
fs.Close();
}
public string Filename
{
get { return attachmentName; }
}
public byte[] DecodedAttachment
{
get { return decodedAttachment; }
}
}
}