zoukankan      html  css  js  c++  java
  • zk 的Fileupload 一直返回NULL 解决办法

    昨天想用Fileupload 控件,可是,上传上去的文件,根本获取不到,从网上查了查,都不行。调试发现,线程没有被阻塞,所以get()方法总是返回null,最后上了官方找到了解决方法。就是捕获Upload事件来获取上传的文件。以下是官方的说明:http://books.zkoss.org/wiki/ZK_Component_Reference/Essential_Components/Fileupload#Event_Thread_Disabled

    ZK Component Reference/Essential Components/Fileupload

    From Documentation

    Jump to: navigation, search

    Search this book:

    TOC

    Essential Components

    Filedownload

    Fisheye

    Contents

    [hide]

    Fileupload

    Employment/Purpose

    There are two ways to use Fileupload as a component to upload files, or invoke Fileupload.get() to open a dialog to upload files.

    Use as a Component

    Fileupload itself is a component. You can use it directly as follows.

    <fileupload label="Upload">

    <attribute name="onUpload">

    org.zkoss.util.media.Media media = event.getMedia();

    //then, you can process media here

    </attribute>

    </fileupload>

    Fileupload is actually a button with upload=true. In other words, the above is equivalent to

    <button label="Upload" upload="true">

    ...

    Please refer to Button: Upload for details.

    Invoke the Static Method: get

    Fileupload provides a set of static methods to simplify file uploading, such as Fileupload.get(), Fileupload.get(String, String), and so on.

    The behavior is a little bit different depending on if the event thread is enabled (default: it is disabled[1]).


    1. Prior to 5.0, it is default to enabled. Refer to ZK Configuration Reference: disable-event-thread.
    Event Thread Disabled

    When the event thread is disabled (default), the execution won't be suspended when Fileupload.get() is called. In other words, the returned value is always null. To retrieve the uploaded files, the developer has to listen the onUpload event, which is sent when the uploading is completed.

    By default, the onUpload event is sent to all root components. For example, Div will, in the following example, receive the onUpload event since it is the root component:(关键的地方)

    <div onUpload="processMedia(event.getMedias());">

    <zscript deferred="true"><![CDATA[

    import org.zkoss.util.media.Media;

    public void processMedia(Media[] media) {

    if (media != null) {

    for (int i = 0; i < media.length; i++) {

    if (media[i] instanceof org.zkoss.image.Image) {

    image.setContent(media[i]);

    } else {

    Messagebox.show("Not an image: " + media[i], "Error",

    Messagebox.OK, Messagebox.ERROR);

    break; //not to show too many errors

    }

    }

    }

    }

    ]]></zscript>

    <vbox>

    <button label="Upload" onClick="Fileupload.get(-1);" />

    <image id="image" />

    </vbox>

    </div>

    Specify the Target Component
    [Since 5.0.2]

    If you prefer the event being sent to a particular component, specify the component in the desktop's attribute calledorg.zkoss.zul.Fileupload.target.

    For example, we could have the button to receive the onUpload event as follows:

    <zk>

    <zscript deferred="true"><![CDATA[

    import org.zkoss.util.media.Media;

    Executions.getCurrent().getDesktop().setAttribute(

    "org.zkoss.zul.Fileupload.target", uploadBtn);

    public void processMedia(Media[] media) {

    if (media != null) {

    for (int i = 0; i < media.length; i++) {

    if (media[i] instanceof org.zkoss.image.Image) {

    image.setContent(media[i]);

    } else {

    Messagebox.show("Not an image: " + media[i], "Error",

    Messagebox.OK, Messagebox.ERROR);

    break; //not to show too many errors

    }

    }

    }

    }

    ]]></zscript>

    <vbox>

    <button id="uploadBtn" label="Upload"

    onUpload="processMedia(event.getMedias());"

    onClick="Fileupload.get(-1);" />

    <image id="image" />

    </vbox>

    </zk>

    Event Thread Enabled

    If the event thread is enable, the uploaded file will be returned directly by Fileupload.get() and other static methods, such as:

    <zk>

    <button label="Upload">

    <attribute name="onClick">{

    org.zkoss.util.media.Media[] media = Fileupload.get(-1);

    if (media != null) {

    for (int i = 0; i &lt; media.length; i++) {

    if (media[i] instanceof org.zkoss.image.Image) {

    org.zkoss.zul.Image image = new org.zkoss.zul.Image();

    image.setContent(media[i]);

    image.setParent(pics);

    } else {

    Messagebox.show("Not an image: "+media[i], "Error", Messagebox.OK, Messagebox.ERROR);

    break; //not to show too many errors

    }

    }

    }

    }</attribute>

    </button>

    <vbox id="pics" />

    </zk>

    As shown, Fileupload.get(int) won't return until the end user uploads the files (and/or closes the dialog).

    Example

    Here is an example that uses Fileupload as a component:

    <image id="img" />

    Upload your hot shot:

    <fileupload label="Upload" onUpload="img.setContent(event.media)" />

    Supported Events

    Name

    Event Type

    None
    None

    • Inherited Supported Events: Button

    Supported Children

    *NONE

    Use Cases

    Version
    Description
    Example Location

    Version History

    Last Update : 2011/7/29

    Version
    Date
    Content

    5.0.2
    May 2010
    Able to specify a target for the onUpload event sent by Fileupload.get(). Used if the event thread is disabled.

  • 相关阅读:
    51Nod-1013 3的幂的和【快速模幂+逆元】
    51Nod-1082 与7无关的数【进制+打表】
    51Nod-1080 两个数的平方和【暴力法】
    51Nod-1015 水仙花数【进制+查表搜索】
    51Nod-1003 阶乘后面0的数量【分析思维】
    51Nod-1002 数塔取数问题【DP】
    51Nod-1179 最大的最大公约数【暴力】
    51Nod-1018 排序【排序】
    51Nod-1126 求递推序列的第N项【递推序列+模除】
    51Nod-1031 骨牌覆盖【递推】
  • 原文地址:https://www.cnblogs.com/OOAbooke/p/2367501.html
Copyright © 2011-2022 走看看