我正在尝试实现GWT图像上传功能。我已经进行了所需的代码更改,但是由于某种原因没有上载。在服务器端,未接收到图像。因此,我在客户端(浏览器)检查了请求标头和内容,然后发现Content-Length:44(仅44)。然后我意识到该图像并未从提交中发送到服务器。请检查下面的GWT代码。
VerticalPanel vp = new VerticalPanel();
vp.add(CommonFormLayoutUtil.createLabel("Upload"));
final FormPanel form = new FormPanel();
form.setAction("CGIImageUpload");
// set form to use the POST method, and multipart MIME encoding.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
final FileUpload fileUpload = new FileUpload();
Button uploadButton = new Button("Upload");
uploadButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
//get the filename to be uploaded
String filename = fileUpload.getFilename();
if (filename.length() == 0) {
showError("No File Specified!", null);
} else {
//submit the form
form.submit();
}
}
});
vp.add(fileUpload);
vp.add(uploadButton);
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed, this
//event is fired. Assuming the service returned a response
//of type text/html, we can get the result text here
showError(event.getResults(), null);
}
});
form.add(vp);
我在这里错过了什么吗?请提出建议。
谢谢。
答案 0 :(得分:2)
FormPanel声明以下内容:
”该面板可用于实现与接受传统HTML表单编码的服务器的互操作性。以下小部件(实现com.google.gwt.user.client.ui.HasName 的那些部件)将如果它们包含在此面板中,则将其提交给服务器”(强调我的意思
您需要设置FileUpload小部件的名称,否则FormPanel将不会提交它。
fileUpload.setName("someName");
尝试设置此项,它应该可以工作