gwt-ext 文件上传
我正在尝试从 gwt-ext 上传文件而不打开对话框。 为此,我创建了一个 FormPanel 并向其中添加了适当的字段。 然后做了一个form.submit()。 这似乎不起作用。 知道为什么吗? 代码如下所示。
final FormPanel uploadForm = new FormPanel();
uploadForm.setVisible(false);
uploadForm.setFileUpload(true);
final TextField sourceFile = new TextField("File", "sourceFile");
sourceFile.setVisible(false);
sourceFile.setInputType("file");
sourceFile.setValue("/tmp/test.txt");
final TextField targetFile = new TextField("Upload As", "targetFile");
targetFile.setVisible(false);
targetFile.setValue("different.txt");
uploadForm.add(sourceFile);
uploadForm.add(targetFile);
final String url = GWT.getModuleBaseURL() + "/uploadFile";
uploadForm.getForm().submit(url, null, Connection.POST, null, false);
我用一个简单的 html 表单在服务器端测试了 servlet,它工作正常。 只有GWT-EXT版本似乎不起作用。
I am trying to do a file upload from gwt-ext without bringing up the dialog box. To do this, I created a FormPanel and added the appropriate fields to it. Then did a form.submit(). This doesn't seem to work. Any idea why? The code is shown below.
final FormPanel uploadForm = new FormPanel();
uploadForm.setVisible(false);
uploadForm.setFileUpload(true);
final TextField sourceFile = new TextField("File", "sourceFile");
sourceFile.setVisible(false);
sourceFile.setInputType("file");
sourceFile.setValue("/tmp/test.txt");
final TextField targetFile = new TextField("Upload As", "targetFile");
targetFile.setVisible(false);
targetFile.setValue("different.txt");
uploadForm.add(sourceFile);
uploadForm.add(targetFile);
final String url = GWT.getModuleBaseURL() + "/uploadFile";
uploadForm.getForm().submit(url, null, Connection.POST, null, false);
I tested the servlet on the server side with a simple html form and it works correctly. Only the GWT-EXT version doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现为什么上面的代码不起作用。 这里的主要问题是,如果上传表单尚未呈现和/或用户单击提交按钮后表单已被修改,则由于安全原因,文件上传会被浏览器阻止。 如果浏览器确实允许这样的事情,那么系统上的任何文件都可以在用户不知情的情况下轻松上传。
解决上述问题的方法是调出对话框,在提交按钮的事件处理程序中执行上传,并在表单侦听器的 onActionComplete 方法中执行任何其他处理。
I found out why the above piece of code is not working. The primary issue here is that file uploads are blocked by the browser due to security reasons if the upload form has not been rendered and/or if the form has been modified after the user clicks the submit button. If the browser did allow such things, then any file on the system can be easily uploaded without the user's knowledge.
Solution to to the above problem is to bring up the dialog box, do the upload in the event handler for the submit button and in the onActionComplete method of the form listener, do any other processing.
在我看来,不使用对话框上传的整个想法似乎是一种安全漏洞。 我可以想象一个应用程序只要打开密码文件就会窃取密码文件,只要上述情况可行即可。
The whole idea of uploading without dialog box looks like a security breach to me. I can imagine an application that steals passwords file whenever opened, if only the above would be possible.