Xpages 上的 Web 标记
Mark of the Web on Xpages
所以我需要向文档发送附件,但我必须验证它是否大于 15mb,因此我在 javascript 中使用此代码来获取文件:
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var filePath = document.getElementById(fileid).value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
我在尝试创建 ActiveXObject 时遇到错误,因为我的站点不是 "trusted " 没有 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>
所以我想知道是否可以在 XPage 中添加 Web 标记,以及如何将它放在 XPage 的正文中。
我的客户不想手动放置安全选项,但想使用IE,请帮帮我哈哈。
如果使用 javascript 选择文件时有另一种检查文件大小的方法会很有趣。
尝试使用此代码检查文件大小 HTML5 应该适用于所有现代浏览器
var fileSize=0
if (typeof FileReader !== "undefined") {
var filePath = document.getElementById(fileid);
fileSize= filePath.files[0].size;
}
检查文件大小变量以了解文件的最大限制。
如果浏览器是 IE10 或更新版本,请使用此代码;如果浏览器较旧,请使用您的旧代码。
您可以为旧浏览器创建 Java 验证器,但如果 Java 脚本 API 可用(现代浏览器),请使用它。
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);
}
}
}
这些验证器需要添加到面中-config.xml
<validator>
<validator-id>attachmentValidator</validator-id>
<validator-class>com.faces.validator.Attachment</validator-class>
</validator>
然后您可以将验证器添加到文件上传字段中:
<xp:this.validators>
<!-- Validator for Attachments -->
<xp:validator validatorId="attachmentValidator">
</xp:validator>
</xp:this.validators>
所以我需要向文档发送附件,但我必须验证它是否大于 15mb,因此我在 javascript 中使用此代码来获取文件:
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var filePath = document.getElementById(fileid).value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
我在尝试创建 ActiveXObject 时遇到错误,因为我的站点不是 "trusted " 没有 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>
所以我想知道是否可以在 XPage 中添加 Web 标记,以及如何将它放在 XPage 的正文中。
我的客户不想手动放置安全选项,但想使用IE,请帮帮我哈哈。
如果使用 javascript 选择文件时有另一种检查文件大小的方法会很有趣。
尝试使用此代码检查文件大小 HTML5 应该适用于所有现代浏览器
var fileSize=0
if (typeof FileReader !== "undefined") {
var filePath = document.getElementById(fileid);
fileSize= filePath.files[0].size;
}
检查文件大小变量以了解文件的最大限制。
如果浏览器是 IE10 或更新版本,请使用此代码;如果浏览器较旧,请使用您的旧代码。
您可以为旧浏览器创建 Java 验证器,但如果 Java 脚本 API 可用(现代浏览器),请使用它。
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);
}
}
}
这些验证器需要添加到面中-config.xml
<validator>
<validator-id>attachmentValidator</validator-id>
<validator-class>com.faces.validator.Attachment</validator-class>
</validator>
然后您可以将验证器添加到文件上传字段中:
<xp:this.validators>
<!-- Validator for Attachments -->
<xp:validator validatorId="attachmentValidator">
</xp:validator>
</xp:this.validators>