---
title: "ZK文件上传组件"
author: "longforfreedom"
date: "2010-03-28T23:41:53Z"
tags: []
categories: ["编程语言"]
slug: "zk"
draft: false
lastmod: "2010-03-28T23:41:53Z"
id: 626803
url: "https://migle.iteye.com/blog/626803"
---

ZUL页面内容 ：

&lt;fileupload id="fileupload" native="true" maxsize="1204"/&gt;

 

maxsize ：文件大小属性， ZK 默认可上传文件大小为 5MB ，不过发现不管设置 个什么值都不起作用看源代码中有

/\**

  * Sets the maximal allowed upload size of the component, in kilobytes.

  * &lt;p&gt;Note: {@link Configuration#setMaxUploadSize(int)} is also allowed to limit the size,

  * if the maximal size is -1.

  * @since 3.6.0

  \*/

public void setMaxsize(int maxsize) {

\_maxsize = maxsize;

}

所以试着用后台代码控制文件上传大小

   public void onCreate(Event event)throws Exception{

Configuration config =       desktop.getWebApp().getConfiguration();

config.setMaxUploadSize( 102400 ); // 单位 KB ，如果为负则不限制大小

}

 

 

native ： /\** Wether to treat the uploaded file(s) as binary. \*/ 默认为 false ，如果需要上传文本格式的中文文件，最好设置为 true ，否则如果以非 UTF8 编码的中文文本文件会变成乱码，所以最好设置为ture

 

 

后台代码

上传文件时会触发 onUpload 事件：

public void onUpload$fileupload(UploadEvent event) throws Exception {

 

media=event.getMedia();

//media = Fileupload.get();

if(media == null){

return;

}

 

fileupload.setVisible(false);

info.setVisible(true);

info.setValue(media.getName());

}

 

 

if(media.isBinary()){

         InputStream input = media.getStreamData();

//attachmentService.save(attachment,media.getStreamData());

}else{

…………………………………………………………………...

}

 

后台读取上传内容的方法有以下四个：getStreamData(),getString(),getStringReader(),getByteData()

根据isBinary()和isMemory()的返回值选择以上四个方法。