0

有没有办法将 SingleUploader 添加到 DynamicForm 对象?我使用 dynamicFormObj.setField() 添加了一些小部件(单选按钮、文本框等)。我想将 SingleUploader 对象与我使用 setField 添加的小部件一起添加到 dynamicFormObj。请让我知道是否有任何解决方案。注意:SingleUploader 允许我们异步上传文件到服务器。这就是我不能使用 UploadItem 的原因。如果有其他使用 DynamicForm 上传文件的替代解决方案,我们也会邀请您。

4

2 回答 2

0

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

于 2013-05-30T12:22:32.110 回答
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 小部件。它对我有用,周末愉快。

于 2013-06-01T17:02:57.297 回答