Hello I am facing kind of similar issue..my problem is that i have a browser enabled infopath 2007 form in that form there is a file attachment control in which users can upload documents nowi want to restrict the file types to be only of doc and pdf ...since selecting allowable file types is not there in browser based forms how do i do that...I got this code from a website but this does'nt work properly..it displays error but it displays for the PDF file also which it should allow..can somebody plz tell me what's wrong with the code...any help is really very much appreciated..
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[] decodeAttachment;
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)
{
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);
// byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
Encoding enc = Encoding.Unicode;
attachmentName = enc.GetString(fileNameBytes, 0, attachmentNameLength );
decodeAttachment = theReader.ReadBytes(fileSize);
}
public string Filename
{
get { return attachmentName; }
}
public byte[] DecodedAttachment
{
get { return decodeAttachment; }
}
}
public void image_Validating(object sender, XmlValidatingEventArgs e)
{
if(!e.UndoRedo && e.Operation == XmlOperation.ValueChange)
{
string base64string = e.Site.Value;
if(!string.IsNullOrEmpty(base64string))
{
InfopathAttachmentDecoder decoder=
new InfopathAttachmentDecoder(base64string);
string filename = decoder.Filename;
byte[] data= decoder.DecodedAttachment;
string fileExtension = filename.Substring(filename.IndexOf(" . ") + 1);
if(fileExtension.ToUpper()!= "PDF")
e.ReportError(e.Site,false,"Only pdf are allowed");
}
}
}
}