ITeye博客

ZK文件上传组件

longforfreedom · 2010-03-28 23:41 · JAVA · 编程语言 · Markdown

ZUL页面内容 :

<fileupload id="fileupload" native="true" maxsize="1204"/>

 

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

/**

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

  * <p>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()的返回值选择以上四个方法。

评论(4)

  1. #4 楼 · longforfreedom · 2014-05-13 11:14

    zhwq1216 writes:

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

    这段说明帮我解决了乱码问题,谢谢!

    很久很久以前用ZK,有用就好

  2. #3 楼 · zhwq1216 · 2014-04-15 23:31

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

    这段说明帮我解决了乱码问题,谢谢!

  3. #2 楼 · zhwq1216 · 2014-04-15 23:27

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

    这段说明帮我解决了乱码问题,谢谢!

  4. #1 楼 · xcltapestry · 2012-12-09 05:15

    :idea: 正好用上,太捧了

评论功能维护中,当前仅展示历史评论。