0

我的项目是在 GWT Java 中使用 Eclipse Juno。

我正在使用 GWTUpload,但是在选择图片后,不显示文件路径和文件名。选择图片后,如何在“浏览”按钮之前显示文件路径和名称?

package org.AwardTracker.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.i18n.client.DateTimeFormat;

import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.UploadedInfo;
import gwtupload.client.MultiUploader;
import gwtupload.client.PreloadedImage;
import gwtupload.client.PreloadedImage.OnLoadPreloadedImageHandler;

import java.io.File;
import java.lang.String;
import java.sql.Date;
import java.util.List;

import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;

public class EditYouthMemberView extends Composite {

private DBConnectionAsync rpc;
private TextBox textBoxFirstName;
private TextBox textBoxScoutNumber;
private ListBox listBoxSection;
private ListBox listBoxPack;
private TextBox textBoxSurname;
private DateBox dateBoxArchived;
private DateBox dateBoxPackIn;
private DateBox dateBoxPackOut;
private DateBox dateBoxDOB;
// A panel where the thumbnails of upload images will be shown
//final FlowPanel panelImages = new FlowPanel();
final HorizontalPanel panelImages = new HorizontalPanel();


@SuppressWarnings("deprecation")
public EditYouthMemberView(final NavHandler navHandler) {       

    final java.util.Date todays_date = new java.util.Date();

    rpc = (DBConnectionAsync) GWT.create(DBConnection.class);
    ServiceDefTarget target = (ServiceDefTarget) rpc;
    String moduleRelativeURL = GWT.getModuleBaseURL() + "MySQLConnection";
    target.setServiceEntryPoint(moduleRelativeURL);

    VerticalPanel verticalPanel = new VerticalPanel();
    verticalPanel.setStyleName("gwt-Banner");
    initWidget(verticalPanel);

    HorizontalPanel horizontalPanel = new HorizontalPanel();
    horizontalPanel.setStyleName("gwt-Banner");
    verticalPanel.add(horizontalPanel);
    horizontalPanel.setWidth("246px");

    Label lblHome = new Label("Home");
    lblHome.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
    });
    horizontalPanel.add(lblHome);
    lblHome.setWidth("46px");

    Label lblEditAwards = new Label("Edit Awards");
    lblEditAwards.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
    });
    horizontalPanel.add(lblEditAwards);
    lblEditAwards.setWidth("85px");

    Label lblListAll = new Label("List all Cub Scouts");
    lblListAll.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
    });
    horizontalPanel.add(lblListAll);
    lblListAll.setWidth("130px");

    Label lblAccountUpdate = new Label("Account Update");
    lblAccountUpdate.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
    });
    horizontalPanel.add(lblAccountUpdate);
    lblAccountUpdate.setWidth("104px");

    FlexTable flexTable_1 = new FlexTable();
    verticalPanel.add(flexTable_1);

    Label lblSurname = new Label("Surname");
    lblSurname.setWordWrap(false);
    lblSurname.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    lblSurname.setStyleName("gwt-Label-Login");
    flexTable_1.setWidget(0, 2, lblSurname);

    Label lblFirstName = new Label("First Name");
    lblFirstName.setWordWrap(false);
    lblFirstName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    lblFirstName.setStyleName("gwt-Label-Login");
    flexTable_1.setWidget(0, 3, lblFirstName);

    textBoxSurname = new TextBox();
    flexTable_1.setWidget(1, 1, textBoxSurname);

    textBoxFirstName = new TextBox();
    flexTable_1.setWidget(1, 2, textBoxFirstName);      

    Label lblLookForPhotograph = new Label("Look for photograph:");
    lblLookForPhotograph.setWordWrap(false);
    lblLookForPhotograph.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    lblLookForPhotograph.setStyleName("gwt-Label-Login");
    flexTable_1.setWidget(2, 1, lblLookForPhotograph);
    flexTable_1.getFlexCellFormatter().setRowSpan(2, 1, 3);

    // Attach the image viewer to the document
    flexTable_1.setWidget(0, 0, panelImages);
    panelImages.setSize("120", "120");
    flexTable_1.getFlexCellFormatter().setRowSpan(0, 0, 4);
    panelImages.clear();

    // Create a new uploader panel and attach it to a document
    final MultiUploader defaultUploader = new MultiUploader();
    flexTable_1.setWidget(3, 1, defaultUploader);
    flexTable_1.getFlexCellFormatter().setRowSpan(3, 1, 3);

    // Add a finish handler which will load the image once the upload finishes
    defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);

    FlexTable flexTable = new FlexTable();
    verticalPanel.add(flexTable);

    Label lblDOB = new Label("Date of Birth:");
    lblDOB.setWordWrap(false);
    lblDOB.setStyleName("gwt-Label-Login");
    flexTable.setWidget(0, 0, lblDOB);
    lblDOB.setDirectionEstimator(false);
    lblDOB.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    lblDOB.setSize("110px", "16px");

    dateBoxDOB = new DateBox();
    dateBoxDOB.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd-MM-yyyy")));
    flexTable.setWidget(0, 1, dateBoxDOB);
    dateBoxDOB.getDatePicker();

    Label lblSection = new Label("Section:");
    lblSection.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    lblSection.setStyleName("gwt-Label-Login");
    flexTable.setWidget(0, 2, lblSection);

    listBoxSection = new ListBox();
    listBoxSection.addItem("Cubs");
    flexTable.setWidget(0, 3, listBoxSection);
    listBoxSection.setVisibleItemCount(1);

    Label lblScoutNumber = new Label("Scout Number:");
    lblScoutNumber.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    lblScoutNumber.setStyleName("gwt-Label-Login");
    flexTable.setWidget(1, 0, lblScoutNumber);
    lblScoutNumber.setDirectionEstimator(false);
    lblScoutNumber.setSize("110px", "16px");

    textBoxScoutNumber = new TextBox();
    flexTable.setWidget(1, 1, textBoxScoutNumber);

    Label lblPack = new Label("Pack:");
    lblPack.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    lblPack.setStyleName("gwt-Label-Login");
    flexTable.setWidget(1, 2, lblPack);

    listBoxPack = new ListBox();
    listBoxPack.addItem("Explorer");
    listBoxPack.addItem("Pioneer");
    flexTable.setWidget(1, 3, listBoxPack);
    listBoxPack.setVisibleItemCount(1);

    Label lblDateArchived = new Label("Date Archived:");
    lblDateArchived.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    lblDateArchived.setStyleName("gwt-Label-Login");
    flexTable.setWidget(2, 0, lblDateArchived);
    lblDateArchived.setSize("110px", "16px");

    dateBoxArchived = new DateBox();
    dateBoxArchived.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd-MM-yyyy")));
    flexTable.setWidget(2, 1, dateBoxArchived);
    dateBoxArchived.getDatePicker();

    Label lblIn_1 = new Label("In:");
    lblIn_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    lblIn_1.setStyleName("gwt-Label-Login");
    flexTable.setWidget(2, 2, lblIn_1);

    dateBoxPackIn = new DateBox();
    dateBoxPackIn.setFormat(new DefaultFormat(DateTimeFormat.getFullDateFormat()));
    flexTable.setWidget(2, 3, dateBoxPackIn);
    dateBoxPackIn.getDatePicker();

    Label lblNewLabel_3 = new Label("Out:");
    lblNewLabel_3.setStyleName("gwt-Label-Login");
    flexTable.setWidget(2, 4, lblNewLabel_3);

    dateBoxPackOut = new DateBox();
    dateBoxPackOut.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd-MM-yyyy")));
    dateBoxPackOut.getDatePicker();

    flexTable.setWidget(2, 5, dateBoxPackOut);

    Button btnAdd = new Button("Add");
    btnAdd.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            Integer tracker = 0;
            if (textBoxSurname.getText().length() == 0) {
                tracker = tracker + 1; 
            }
            if (textBoxFirstName.getText().length() == 0) {
                tracker = tracker + 2; 
            }
            // if (panelImages.getFilename().length() == 0) {
            //  tracker = tracker + 4; 
            // }
            if (dateBoxDOB.toString().length() == 0) {
                tracker = tracker + 8; 
            }
            if (dateBoxPackIn.toString().length() == 0) {
                tracker = tracker + 16; 
            }

            if (tracker == 1) {
                Window.alert("Please enter a Surname.");
            }
            if (tracker == 2) {
                Window.alert("Please enter a First Name.");
            }
            if (tracker == 3) {
                Window.alert("Please enter Surname and First Name");
            }
            if (tracker == 4) {
                Window.alert("Please select a photograph.");
            }
            if (tracker == 5) {
                Window.alert("Please enter a Surname and select a photograph.");
            }
            if (tracker == 6) {
                Window.alert("Please enter a FirstName and select a photograph.");
            }
            if (tracker == 7) {
                Window.alert("Please enter Surname, First Name and select a photograph");
            }
            if (tracker == 8) {
                Window.alert("Please enter a DOB");
            }
            if (tracker == 9) {
                Window.alert("Please enter a Surname and DOB");
            }
            if (tracker == 10) {
                Window.alert("Please enter a First Name and DOB");
            }
            if (tracker == 11) {
                Window.alert("Please enter a Surname, First Name and DOB");
            }
            if (tracker == 12) {
                Window.alert("Please select a photograph and enter a BOB");
            }
            if (tracker == 13) {
                Window.alert("Please enter a Surname, select a photograph and enter a DOB");
            }
            if (tracker == 14) {
                Window.alert("Please enter a FirstName, select a photograph and enter a DOB");
            }
            if (tracker == 15) {
                Window.alert("Please enter Surname, First Name, select a photograph and enter a DOB");
            }
            if (tracker == 16) {
                Window.alert("Please enter date joined Pack");
            }

            if (tracker == 17) {
                Window.alert("Please enter a Surname and date joined Pack.");
            }
            if (tracker == 18) {
                Window.alert("Please enter a First Name and date joined Pack.");
            }
            if (tracker == 19) {
                Window.alert("Please enter Surname, First Name and date joined Pack");
            }
            if (tracker == 20) {
                Window.alert("Please select a photograph and enter date joined Pack.");
            }
            if (tracker == 21) {
                Window.alert("Please enter a Surname, select a photograph and enter date joined Pack.");
            }
            if (tracker == 22) {
                Window.alert("Please enter a First Name, select a photograph and enter date joined Pack.");
            }
            if (tracker == 23) {
                Window.alert("Please enter Surname, First Name, select a photograph and enter date joined Pack");
            }
            if (tracker == 24) {
                Window.alert("Please enter a DOB and date joined Pack");
            }
            if (tracker == 25) {
                Window.alert("Please enter a Surname, DOB and date joined Pack");
            }
            if (tracker == 26) {
                Window.alert("Please enter a First Name, DOB and date joined Pack");
            }
            if (tracker == 27) {
                Window.alert("Please enter a Surname, First Name, DOB and date joined Pack");
            }
            if (tracker == 28) {
                Window.alert("Please select a photograph, enter a BOB and date joined Pack");
            }
            if (tracker == 29) {
                Window.alert("Please enter a Surname, select a photograph, enter a DOB and date joined Pack");
            }
            if (tracker == 30) {
                Window.alert("Please enter a FirstName, select a photograph, enter a DOB and date joined Pack");
            }
            if (tracker == 31) {
                Window.alert("Please enter Surname, First Name, select a photograph, enter a DOB and date joined Pack");
            }

            //if (tracker == 0) {
            //  AsyncCallback<YthMmbrSectDtls> callback = new CreationHandler<YthMmbrSectDtls>(null);
            //  rpc.createYouthMember(null, textBoxSurname.getText(), textBoxFirstName.getText(), dateBoxDOB.getValue(), 
            //          fileUpload.getFilename(), dateBoxArchived.getValue(), null, listBoxSection.getItemText(listBoxSection.getSelectedIndex()),
            //          listBoxPack.getItemText(listBoxPack.getSelectedIndex()), dateBoxPackIn.getValue(), 
            //          dateBoxPackOut.getValue(), callback);
            //}

        }

    });
    flexTable.setWidget(3, 0, btnAdd);

    Button btnUpdate = new Button("Update");
    btnUpdate.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
    });
    btnUpdate.setText("Update");
    flexTable.setWidget(3, 1, btnUpdate);

    Button btnCancel = new Button("Cancel");
    btnCancel.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
    });
    btnCancel.setText("Cancel");
    flexTable.setWidget(3, 2, btnCancel);

    Button btnClearPage = new Button("Clear Page");
    btnClearPage.setText("Clear Page");
    btnClearPage.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            textBoxSurname.setText("");
            textBoxFirstName.setText("");
            dateBoxDOB.setValue(null);
            dateBoxPackIn.setValue(todays_date);
            dateBoxPackOut.setValue(null);
            textBoxScoutNumber.setText("");
            dateBoxArchived.setValue(null);
        }
    });
    flexTable.setWidget(3, 3, btnClearPage);
    setStyleName("body");
}

// Load the image in the document and in the case of success attach it to the viewer
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
    public void onFinish(IUploader uploader) {
        if (uploader.getStatus() == Status.SUCCESS) {
            new PreloadedImage(uploader.fileUrl(), showImage);

            //The server sends useful information to the client by default
            UploadedInfo info = uploader.getServerInfo();
            System.out.println("File name " + info.name);
            System.out.println("File content-type " + info.ctype);
            System.out.println("File size " + info.size);

            // You can send any customised message and parse it
            System.out.println("Server message " + info.message);
        }
    }
};

// Attach an image to the pictures viewer
private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
    public void onLoad(PreloadedImage image) {
        image.setWidth("75px");
        panelImages.add(image);
    }
};

/**
 * Decodes the base64 string into byte array to display image
 * Calls Base64Decode.java
 * @param imageDataString - a {@link java.lang.String}
 * @return byte array
 */
public static byte[] decodeImage(String imageDataString) {
    return Base64Decode.decode(imageDataString);
    }

public IUploader.OnFinishUploaderHandler getOnFinishUploaderHandler() {
    return onFinishUploaderHandler;
}

public void setOnFinishUploaderHandler(IUploader.OnFinishUploaderHandler onFinishUploaderHandler) {
    this.onFinishUploaderHandler = onFinishUploaderHandler;
}

}

这就是发生的事情:我选择照片的开始屏幕 在此处输入图像描述

我选择一张照片,它会自动开始加载。在加载时可以看到路径 在此处输入图像描述

一旦照片完成自动显示文件路径就会消失。 在此处输入图像描述

4

2 回答 2

1

MultiUploader 侦听底层输入元素上的更改事件,并自动将所选文件上传到服务器。然后它会创建一个新表单,以便可以选择更多文件。因此,您将永远无法在“浏览”按钮旁边看到位置/名称。

看起来您只想选择一个文件(如果我正确阅读了您的代码),您可能会考虑使用 IUploader 的不同实现,例如gwtupload.client.SingleUploader.

于 2013-06-10T04:35:35.607 回答
1

使用 MultiUploader,每个上传的文件名都显示在浏览器按钮之前的一行中。

您必须在应用程序中包含 Upload.css 文件,通常通过 module.gwt.xml 完成。

但是,由于安全限制,某些浏览器中使用的 FileInput 不会返回文件路径,因此未显示文件路径。所以为了连贯,它只显示名称。

顺便说一句:我已经测试了您的代码,并且得到了所选文件的名称(显然不是路径)。

[编辑]

您正在使用本机input file,gwtupload 会在上传完成时重置输入文件,因此这就是您看不到文件名的原因。在您的情况下,您需要一个标签来在文件发送后更新文本。

[编辑-2]

这可能对你有用:

final Label fileName = new Label();
flexTable_1.setWidget(n, n , fileName);

defaultUploader.addOnFinishUploadHandler(new OnFinishUploaderHandler() {
  public void onFinish(IUploader uploader) {
    if (uploader.getStatus() == Status.SUCCESS) {
        fileName.setText(uploader.getFileName);
    }
  }
});
于 2013-06-17T05:41:12.057 回答