【发布时间】:2019-06-16 02:20:55
【问题描述】:
我正在使用 GWT FileUpload() 和一个表单来让用户选择图像并将其上传到服务器。我想要做的是在图像到达服务器之前预览图像。我只能从 FileUpload() 获取文件名
HorizontalPanel row = new HorizontalPanel();
row.add(accounts = new ListBox());
accounts.setWidth("200px");
row.setStyleName("accountPositioning");
accounts.setName("accounts");
final Image image = new Image();
image.setStyleName("previewImage");
setCellSpacing(10);
panel = new VerticalPanel();
panel.add(row);
panel.add(image);
final FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
downloadPanel = new FormPanel();
downloadPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
downloadPanel.setMethod(FormPanel.METHOD_GET);
deletePanel = new FormPanel();
deletePanel.setEncoding(FormPanel.ENCODING_MULTIPART);
deletePanel.setMethod(FormPanel.METHOD_POST);
upload = new FileUpload();
upload.setName("upload");
upload.setStyleName("chooseImageButton");
upload.setEnabled(false);
upload.setVisible(false);
VerticalPanel holder = new VerticalPanel();
uploadButton = new Button("Import");
uploadButton.setEnabled(false);
uploadButton.setStyleName("importImageButton");
uploadButton.setVisible(false);
uploadButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String filename = upload.getFilename();
if (filename.length() == 0) {
Window.alert("No File Specified!");
} else {
int selectedIndex = accounts.getSelectedIndex();
accountIdStr = accounts.getValue(selectedIndex);
form.setAction(GWT.getModuleBaseURL()+"uploadfile" + "?entityId="+ accountIdStr);
form.submit();
}
}
});
如何使用 GWT FileUpload() 获取图像文件上传的文件路径,以便在将图像提交到服务器之前预览图像?
我使用的是 GWT 2.7.0 版本,所以我无法使用文件或路径库
【问题讨论】:
-
我试过了。它从未到达 reader.onload。它读取了这一行并停止了 reader.readAsDataURL(image);此外,没有提供 imageLoaded() 方法,我无法在 loadImage() 中获得要识别的 java 方法
标签: java file-upload gwt