无法访问vaadin FileUpload FinishListener中的UI元素

时间:2015-10-14 01:13:48

标签: java google-app-engine file-upload vaadin

我正在尝试提供在我的vaadin应用程序中上传文件的功能

questionImageUpload.addFinishedListener(new Upload.FinishedListener() { @Override public void uploadFinished(Upload.FinishedEvent event) { boolean hasLock = VaadinSession.getCurrent().hasLock(); button.setEnabled(false); } });

FinishListener.uploadFinished()

但是,在我的VaadinSession中,如果我修改了一些UI元素(在上面,我禁用了一个按钮),则修改不会被应用。

我假设可以在非UI线程中调用此方法,因此我通过在上面的uploadFinished中放置一个断点来检查VaadinSession.getCurrent()是否可用。但是,hasLock没有返回null。 <?php // sample text to parse $some_text = '"DENO;NAME;SURNAME;""BIRTH"";""ZIP"";CITY;E-MAIL;TELEPHONE" "M;DAVID;BON;""1959-02-12 00:00:00"";75009;PARIS;email@gmail.com;010000000" "M;DOE;JHON;""1947-02-02 00:00:00"";75008;PARIS;email@gmail.com;060000000" "M;DAVE;Philippe;""1950-01-01 00:00:00"";75002;""PARIS"";email@gmail.com;070000000"'; // remove quotes $final_text = str_replace('"', '', $some_text); // create array of rows by searching for new lines $data = str_getcsv($final_text, "\n"); // create an empty array to save our final csv data $final_csv = array(); // loop thru the array and save to the final csv data foreach ($data as $value) { // before saving to final csv data, split row into individual column items $value_array = explode(";", $value); $final_csv[] = $value_array; } // helper debugger to show data before writing it echo "<pre>"; print_r($final_csv); echo "</pre>"; // create a new file for writing or open and truncate to 0 $fp = fopen('file.csv', 'w'); // write to file from final csv data foreach ($final_csv as $fields) { fputcsv($fp, $fields); } // close file fclose($fp); ?> 也是如此。

可能是什么原因?

我在Google App Engine上运行此vaadin应用程序(仍然在IntelliJ IDEA内部运行)。这可能是背后的原因吗?

2 个答案:

答案 0 :(得分:1)

文件上传作为对服务器的POST请求完成,包含文件数据。上传完成后,在该POST请求结束时调用Upload.FinishedListeners。虽然所有线程本地都已正确设置,但这不是UI更新请求(或UIDL请求),并且发送到浏览器的响应仅包含一个文本,通知浏览器上载完成。任何完成的UI更新都将排队,直到另一个请求要求它们为止。

因此,您需要使用@Push,以便通过推送通道立即将UI更改推送到客户端,或者在开始上载时最迟启用轮询,以便轮询请求将恢复用户界面更改。

答案 1 :(得分:0)

我实际上完成了你想要用SuccedListener做的事情。我有一个代码,用于更新已上传图片的嵌入式组件。您可以查看代码并从中获取提示。它也可以禁用该按钮。你可以纠正它没有优化,但它的工作

public class PicUploader extends Upload实现SucceededListener,Receiver {

private static final long serialVersionUID = 1L;
File file;

public String fileName;
final String LOCATION = "/home/###";

Embedded image = new Embedded();
TextField field;

public PicUploader(Embedded image, String caption) {
    this.image = image;
    this.addSucceededListener(this);
    this.setReceiver(this);
    this.setCaption(caption);

    this.setIcon(FontAwesome.UPLOAD);;

}

public PicUploader(Embedded image, TextField field) {
    this.image = image;
    this.addSucceededListener(this);
    this.setReceiver(this);
    this.field = field;
    this.setButtonCaption(""+FontAwesome.UPLOAD);
    this.setIcon(FontAwesome.UPLOAD);;

}

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
    // TODO Auto-generated method stub
    FileOutputStream stream = null;

    try {
        file = new File(LOCATION
                + "/Pictures/"
                + System.currentTimeMillis()
                + filename.substring(filename.length() - 4,
                        filename.length()));
        fileName = file.getName();
        System.out.println("This is the file name " + filename);
        stream = new FileOutputStream(file);
    } catch (FileNotFoundException ex) {

        ex.printStackTrace();
    }

    return stream;
}

@Override
public void uploadSucceeded(SucceededEvent event) {
    // TODO Auto-generated method stub
    image.setSource(new FileResource(file));
    image.setVisible(true);
    image.setWidth("150");
    image.setHeight("200");
    // float ratio = image.getHeight()/image.getWidth();
    // image.setWidth(""+image.getWidth());
    // image.setHeight(""+image.getHeight());
    // field.setValue(getFileName());
    this.setEnabled(false);
}

public String getFileName() {
    return fileName;
}

}