页面代码
<h:form id="fileForm">
<rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" id="upload"
onclear="clearFile(event);"/>
</h:form>
<a4j:jsFunction name="removeFile" action="#{fileUploadBean.clearFile}">
<a4j:param name="filename" assignTo="#{fileUploadBean.filename}"/>
</a4j:jsFunction>
<h:outputScript>
var clearFile = function (event) {
var data = event.rf.data;
for (var i in data) {
var item = data[i];
removeFile(item.name);
}
}
</h:outputScript>
Managed Bean 代码
public class FileUploadBean {
private Map<String, UploadedImage> files = new HashMap<>();
private String filename;
public void listener(FileUploadEvent event) throws Exception {
UploadedFile item = event.getUploadedFile();
UploadedImage file = new UploadedImage();
file.setLength(item.getData().length);
file.setName(item.getName());
file.setData(item.getData());
files.put(item.getName(), file);
}
public void clearFile() {
files.remove(filename);
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public static class UploadedImage implements Serializable {
private static final long serialVersionUID = -8192553629588066292L;
private String name;
private long length;
private byte[] data;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}
}