Vaadin Flow:如何使用拖动的文件

发布于 2025-01-13 04:55:43 字数 226 浏览 1 评论 0原文

我有一个部分(DropTarget),用户可以在其中从我的应用程序中删除多个项目。 这很好用。

现在我还想允许用户将文件拖到该 DropTarget。

当我将文件拖动到 DropTarget 时,我注册的放置侦听器会收到通知,但是 - 据我所知 - 不提供任何使用拖动的文件的可能性。

有人知道如何让它运行吗?

使用 Vaadin 流 22.0.7

I have a section (DropTarget) where the user can drop several items from within my application.
This works fine.

Now I would also like to allow the user to drag files to that DropTarget.

The drop listener that I registered gets notified when I drag a file to the DropTarget, but - as far as I see - does not offer any possibility to consume the dragged file.

Anybody knows how to get this running?

Using Vaadin flow 22.0.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2025-01-20 04:55:43

创建上传组件时,您可以指定一个接收器。您可以将其作为构造函数参数或通过 upload.setReceiver(Receiver) 传递。根据您的使用情况,有不同类型的接收器;如果您同意将所有数据放入服务器内存中,则可以使用 MemoryBuffer,但还有其他选项,例如 FileBuffer,如下所示:< a href="https://vaadin.com/docs/latest/ds/components/upload/#handling-uploaded-files-java-only" rel="nofollow noreferrer">https://vaadin.com/docs/latest/ds/components/upload/#handling-uploaded-files-java-only ;您也可以实现自己的Receiver

Receiver 使您可以访问文件的实际流内容。通常,您希望在上传过程的某个阶段访问数据,这可以通过不同的上传侦听器来完成。如果您只想在上传完全完成后处理它,则可以使用 SucceededListener

        MemoryBuffer memoryBuffer = new MemoryBuffer();
        Upload upload = new Upload(memoryBuffer);
        upload.addSucceededListener(event -> {
            InputStream fileData = memoryBuffer.getInputStream();
            String fileName = event.getFileName();
            File targetFile = new File("C:/tmp/" + fileName );
            OutputStream outStream = null;
            try {
                outStream = new FileOutputStream(targetFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                outStream.write(fileData.readAllBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

实现您自己的 Receiver 可以让您更灵活地处理如何处理来自上传的 OutputStream ,当然你可能不想将上传保存为物理文件,而是直接将其放入数据库中。

When you create an Upload component, you can specify a Receiver. You can pass one as a constructor parameter or via upload.setReceiver(Receiver). There are different types of Receivers depending on your use case; you can use a MemoryBuffer if you are ok with putting all of the data in your server memory, but there are other options, like FileBuffer, as can be seen here: https://vaadin.com/docs/latest/ds/components/upload/#handling-uploaded-files-java-only ; you can implement your own Receiver as well.

The Receiver gives you access to the actual streaming content of the file. Typically, you want to access the data in some stage of the upload process, which you can do through different upload listeners. If you just want to deal with it once the upload is fully complete, you can use a SucceededListener:

        MemoryBuffer memoryBuffer = new MemoryBuffer();
        Upload upload = new Upload(memoryBuffer);
        upload.addSucceededListener(event -> {
            InputStream fileData = memoryBuffer.getInputStream();
            String fileName = event.getFileName();
            File targetFile = new File("C:/tmp/" + fileName );
            OutputStream outStream = null;
            try {
                outStream = new FileOutputStream(targetFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                outStream.write(fileData.readAllBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

Implementing your own Receiver gives you more flexibility on how you want to handle the OutputStream from the upload, and of course you might not want to save the upload as a physical file, but put it directly in a database for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文