SingleUploader到DynamicForm的任何方式

时间:2013-05-30 07:14:40

标签: smartgwt

有没有办法将SingleUploader添加到DynamicForm对象? 我使用dynamicFormObj.setField()添加了一些小部件(Radiobutton,TextBox等)。我想将SingleUploader对象与我使用setField添加的小部件一起添加到dynamicFormObj。如果有任何解决方案,请告诉我。 注意:SingleUploader允许我们异步上传文件到服务器。这就是我无法使用UploadItem的原因。如果有任何替代解决方案使用DynamicForm上传文件也会被邀请。

2 个答案:

答案 0 :(得分:0)

我建议你看看FileItem。此外,不是在构造之后尝试将其添加到表单中,而是在开头添加它,然后在要显示或不显示时显示/隐藏。显示/隐藏的另一个建议是使用showIfFunction

答案 1 :(得分:0)

我有一个解决方案,通过使用隐藏的Gwt小部件将上传逻辑绑定到IButton。

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.smartgwt.client.util.ValueCallback;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

/**
 * The FileUploadButton widget class is a class that implements the same APIs as the
 * {@link com.smartgwt.client.widgets.IButton} class. Once the button is clicked, a "Open" dialog will be shown. After a
 * file is selected, the file content will be uploaded to the destination. The response will be shown, if a callback
 * function is assigned.
 * 
 * Depending on the current skin, <code>IButton</code>s may be on the
 * {@link com.smartgwt.client.widgets.StretchImgButton} component, which renders via images, or may be based on the
 * {@link com.smartgwt.client.widgets.Button} component, which renders via CSS styles.
 */
public class FileUploadButton extends IButton {

    private String filename;
    private ValueCallback callback;

    public FileUploadButton(String action, String fieldName, String title) {
        setTitle(title);
        createAndInitUploadForm(action, fieldName);
    }

    /**
     * Set the accept types (e.g. text/plain) of files, which will affect the file extension filter of "Open" dialog.
     */
    public void setAccpetTypes(String types) {
        fileUpload.getElement().setAttribute("accept", types);
    }

    /**
     * Set the callback when the submission is completed.
     */
    public void setCallback(ValueCallback callback) {
        this.callback = callback;
    }

    /**
     * Return the filename that has been chosen or an empty string.
     */
    public String getFileName() {
        return filename;
    }

    private FormPanel form; // GWT widget
    private FileUpload fileUpload; // GWT widget

    private static native void clickOnInputFile(Element elem) /*-{
        elem.click();
    }-*/;

    private void createAndInitUploadForm(String action, String fieldName) {
        form = new FormPanel();
        form.setAction(action);
        form.setMethod(FormPanel.METHOD_POST);
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
            public void onSubmitComplete(SubmitCompleteEvent event) {
                setDisabled(false);
                if (callback != null) {
                    callback.execute(event.getResults());
                }
            }
        });

        fileUpload = new FileUpload();
        fileUpload.setName(fieldName);
        fileUpload.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                filename = fileUpload.getFilename();
                if (!filename.isEmpty()) {
                    setDisabled(true);
                    form.submit();
                }
            }
        });
        form.setWidget(fileUpload);

        // Trick to draw an invisible form on the button canvas. Note that the width and the height cannot be a
        // negative or zero, because it will cause the form not to be created onto the canvas!
        form.setVisible(false);
        form.setWidth("1px");
        form.setHeight("1px");

        addChild(form);

        addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                clickOnInputFile(fileUpload.getElement());
            }
        });
    }

}

您可以根据需要将IButton更改为任何其他SmartGwt小部件。它适合我,周末愉快。