Raphael Maycon
Raphael Maycon

Reputation: 73

Mark of the Web on Xpages

So I need to send an attachment to a document, but I have to validate if it is larger than 15mb , for so I am using this code in javascript to get the file :

var objFSO = new ActiveXObject("Scripting.FileSystemObject"); 
            var filePath = document.getElementById(fileid).value;
            var objFile = objFSO.getFile(filePath);
            var fileSize = objFile.size; //size in kb

I'm having an error when I try to create ActiveXObject because my site is not "trusted " by not having a Mark of the Web

<!doctype html>
<!-- saved from url=(0023)http://www.contoso.com/ -->
<html>
  <head>
    <title>A Mark of the Web Example.</title>
  </head>
  <body>
     <p>Hello, World</p>
  </body>
</html>

so I wonder if it is possible to have a mark of the web in a XPage and how I could put it the body of the XPage.

My client does not want to manually place the security option , but want to use IE , please help me haha.

If there is another way to check the file size when selecting a file using javascript would be interesting.

Upvotes: 1

Views: 308

Answers (2)

Txemanu
Txemanu

Reputation: 449

You can create a Java validator for old browsers, but if the Javascript API is available (modern browsers), use it.

public class Attachment implements Validator {

    private final static long BYTES_IN_1_MB     = 1048576;
    private final static long MAX_MB_ALLOWED    = 10;
    private final static String MSG_ERROR_SIZE  = "File size cannot be bigger than {0} MBs";

    public void validate(FacesContext fc, UIComponent uiComp, Object attach)
            throws ValidatorException {     

        FacesMessage msg;

        UploadedFile upFile = (UploadedFile) attach;
        long max_bytes = BYTES_IN_1_MB * MAX_MB_ALLOWED;

        // SIZE:        
        if (upFile.getContentLength() > max_bytes) {
            String msgError = MSG_ERROR_SIZE.replace("{0}", String.valueOf(MAX_MB_ALLOWED));
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msgError, msgError);        
            throw new ValidatorException(msg);
        }       

    }   

}

These validators need to be added into the faces-config.xml

  <validator>
    <validator-id>attachmentValidator</validator-id>
    <validator-class>com.faces.validator.Attachment</validator-class>
  </validator>

Then you can add the validator into the fileUpload field:

<xp:this.validators>                                    
    <!--    Validator for Attachments   -->
    <xp:validator validatorId="attachmentValidator">
    </xp:validator>
</xp:this.validators>

Upvotes: 0

Fredrik Norling
Fredrik Norling

Reputation: 3484

Try this code to check file size in HTML5 should work in all modern browsers

var fileSize=0
if (typeof FileReader !== "undefined") {
    var filePath = document.getElementById(fileid);
  fileSize= filePath.files[0].size; 
}

Check the filesize var for the max limit of you file.

Use this code if the browser is IE10 or newer and your old code if the browser is older.

Upvotes: 4

Related Questions