Pb checking type and size on file attached with Infopath while on server form (web)

  • Thread starter michael.bouvard
  • Start date
M

michael.bouvard

I'd like to check FileAttached information when validating the form as
the user submit it.
File type (only pictures actually and size of the file)

But when I use "File Attachment Properties" the box "Allow the user to
attach only the following file types" is deactivated and a message is
written saying "Setting allowable file types is not supported in
server forms".

Then I try to use some code instead as :

---------------------------
public void FileAttached_Validating (object sender,
XmlValidatingEventArgs e)
{

string[] authorizedExtension = { "jpg", "jpeg", "png",
"gif", "bmp", "tif", "swf" };
string xPath = "/my:myFields/my:FileAttached"; // the
control to be checked
string extensionOfFileAttached =
GetExtensionOfFileAttached(xPath);
string validatingName = "FileAttachedValidating"; //this
name must be unique and specific to this type of field
string message = string.Empty;
foreach (string extension in authorizedExtension)
{
message += extension;
message += " ";
}
string messageDetail = string.Empty;
RaiseErrorIfExtensionIsNotAuthorized(authorizedExtension,
extensionOfFileAttached, validatingName, xPath, message,
messageDetail);


}
--------------------

When I attach a *.gif file (advisors.gif) for instance and try to
submit the form :

But as soon as the function GetExtensionOfFileAttached(xPath) is reach
(which is just returning the
myString.Substring(myString.LastIndexOf(".") ) I got an error cause
the xPath in that case doesn't contain the
full path of the file (or even the name of the file attached) but
something looking as :

x0lGQRQAAAABAAAAAAAAADY2AAANAAAAYQBkAHY..... (ECT) ... W+P
+9zrfve8773vfw/8AIQAADs=

the byte[] of the file I guess.

Even in my form (under preview) I can see the name and extension of
the file : I can see "advisors.gif" "13.55 KB".

If I looked on the "View Source" I can find this information :

var g_objCurrentFormData = [[],[1,[[[2,[0,["","","",-1],[[3,[[[4,[0,
["","","",-1], ... . (ECT....)
[[[24,[0,["x0lGQRQAAAABAAAAAAAAADY2AAANAAAAYQBkAHYAaQ(ECT....)
002b97W\u002bP\u002b9zrfve8773vfw\u002f8AIQAADs=","","",-1],[[25,
["FileData",["FileData".... (ECT.....)ou8Zag4XYnCBsJ4R0R14b9yA==|
633222634800956462"];


How can I get the extension file and its size (properly) ?

Thanks
 
S

S.Y.M. Wong-A-Ton

Once you attach a file, it is saved as a base 64 encoded string in the form.
You can decode this information, i.e., convert the file back to binary data
(see http://support.microsoft.com/kb/892730). Once you've done this, the
header of the file will contain information on the file size and the file
name. I do not think the file type is stored anywhere, but you should be able
to deduce it from the name. This is not a reliable way of going about it,
though, since file types can be 'faked'. However, if you only need to detect
images, you could use classes in the System.Drawing namespace to determine
whether a file is an image or not. See
http://blogs.msdn.com/infopath/archive/2004/03/18/92221.aspx for information
on the header.
---
S.Y.M. Wong-A-Ton


I'd like to check FileAttached information when validating the form as
the user submit it.
File type (only pictures actually and size of the file)

But when I use "File Attachment Properties" the box "Allow the user to
attach only the following file types" is deactivated and a message is
written saying "Setting allowable file types is not supported in
server forms".

Then I try to use some code instead as :

---------------------------
public void FileAttached_Validating (object sender,
XmlValidatingEventArgs e)
{

string[] authorizedExtension = { "jpg", "jpeg", "png",
"gif", "bmp", "tif", "swf" };
string xPath = "/my:myFields/my:FileAttached"; // the
control to be checked
string extensionOfFileAttached =
GetExtensionOfFileAttached(xPath);
string validatingName = "FileAttachedValidating"; //this
name must be unique and specific to this type of field
string message = string.Empty;
foreach (string extension in authorizedExtension)
{
message += extension;
message += " ";
}
string messageDetail = string.Empty;
RaiseErrorIfExtensionIsNotAuthorized(authorizedExtension,
extensionOfFileAttached, validatingName, xPath, message,
messageDetail);


}
--------------------

When I attach a *.gif file (advisors.gif) for instance and try to
submit the form :

But as soon as the function GetExtensionOfFileAttached(xPath) is reach
(which is just returning the
myString.Substring(myString.LastIndexOf(".") ) I got an error cause
the xPath in that case doesn't contain the
full path of the file (or even the name of the file attached) but
something looking as :

x0lGQRQAAAABAAAAAAAAADY2AAANAAAAYQBkAHY..... (ECT) ... W+P
+9zrfve8773vfw/8AIQAADs=

the byte[] of the file I guess.

Even in my form (under preview) I can see the name and extension of
the file : I can see "advisors.gif" "13.55 KB".

If I looked on the "View Source" I can find this information :

var g_objCurrentFormData = [[],[1,[[[2,[0,["","","",-1],[[3,[[[4,[0,
["","","",-1], ... . (ECT....)
[[[24,[0,["x0lGQRQAAAABAAAAAAAAADY2AAANAAAAYQBkAHYAaQ(ECT....)
002b97W\u002bP\u002b9zrfve8773vfw\u002f8AIQAADs=","","",-1],[[25,
["FileData",["FileData".... (ECT.....)ou8Zag4XYnCBsJ4R0R14b9yA==|
633222634800956462"];


How can I get the extension file and its size (properly) ?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top