<button label="上传 Image" upload="true,maxsize=1073741824">
<attribute name="onUpload"><![CDATA[
org.zkoss.util.media.Media media = event.getMedia();
String fileName = "";
if(media!=null)
fileName = media.getName();
三种流:
java.io.InputStream ins = new java.io.ByteArrayInputStream(media.getStringData().getBytes());
java.io.InputStream ins = new java.io.ByteArrayInputStream(media.getByteData());
java.io.InputStream ins = new java.io.BufferedInputStream(media.getStreamData());
java.io.File f = new java.io.File("D:\\temp\\"+fileName);
if(!f.exists()){
f.createNewFile();
}
java.io.OutputStream out = new java.io.FileOutputStream(f);
byte[] buf=new byte[1024];
int len;
while((len=ins.read(buf))>0){
out.write(buf,0,len);
}
out.close();
ins.close();
alert("上传成功");
]]></attribute>
</button>
上传按扭监听器写法:
uploadBtn.addEventListener(Events.ON_UPLOAD, new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
// TODO Auto-generated method stub
UploadEvent uploadEvent = (UploadEvent) event;
Media media = uploadEvent.getMedia();
}
});
解决ZK文件上传中文问题:
第二种写法(zk8.0.0.1):
<a id="fileUploadA" label="导出文件"></a>
后台:
fileUploadA.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
@Override
public void onEvent(Event event) throws Exception {
Fileupload.get("选择文件", "上传", 2, 1000000, true);
Executions.getCurrent().getDesktop().setAttribute(
"org.zkoss.zul.Fileupload.target", fileUploadA);
}
});
fileUploadA.addEventListener(Events.ON_UPLOAD, new EventListener<Event>() {
@Override
public void onEvent(Event event) throws Exception {
UploadEvent uploadEvent = (UploadEvent) event;
alert(uploadEvent.getMedias().length+"");
}
});
效果: