【问题标题】:GWT Image Upload empty contentGWT 图片上传空内容
【发布时间】:2018-10-11 09:56:55
【问题描述】:

我正在尝试实现 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);

我在这里有什么遗漏吗?请提出建议。

谢谢。

【问题讨论】:

    标签: file-upload gwt image-uploading image-upload


    【解决方案1】:

    FormPanel 声明如下:

    “此面板可用于实现与接受传统 HTML 表单编码的服务器的互操作性。以下小部件(那些实现 com.google.gwt.user.client.ui.HasName)将如果它们包含在此面板中,则将它们提交给服务器”(强调我的)

    需要设置FileUpload小部件的名称,否则FormPanel不会提交。

    fileUpload.setName("someName");
    

    尝试设置它,它应该可以工作

    【讨论】:

    • 非常感谢您的回复,您的回答是正确的。互联网上的所有示例都没有提供名称属性,这实际上误导了我。无论如何,再次感谢。
    • 我需要一些有关 GWT FileUpload 中可用功能的帮助。 1.是否可以获得图像的大小。 2. 是否可以获取图像的像素大小。
    • 你是指上传文件前的客户端还是服务器端?
    • 上传文件前的客户端。抱歉没有具体说明。
    • 这可能对stackoverflow.com/questions/16883231/…有帮助。您无法在服务器端控制这些事情并让客户端知道的任何特殊原因?
    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多