简体   繁体   English

GWT文件上传

[英]GWT file upload

I have a following problem - we are using FormPanel which sends file to the Servlet which takes the arguments and tries to parse XML from this file. 我有一个以下问题-我们正在使用FormPanel ,它将文件发送到Servlet,后者使用参数并尝试从该文件解析XML This works fine. 这很好。

Problem is when the user uploaded a wrong file, so parsing ends with SAXException which I would like to propagate (or the exception's message) to client. 问题是用户上载错误的文件时,因此解析以SAXException结尾,我想将其传播(或异常的消息)到客户端。 I tried something like 我尝试了类似的东西

catch (SAXException ex) {
    response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
    response.flushBuffer();
}

but it's not working, I always get empty tag pre ( <pre></pre> ). 但它不起作用,我总是得到空标签pre( <pre></pre> )。 I am trying to catch this with 我试图抓住这个

formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {

    @Override
    public void onSubmitComplete(SubmitCompleteEvent event) {
        String s = event.getResults();
    });

I can use response.getWriter().write("Error"); 我可以使用response.getWriter().write("Error"); in my Servlet but how the client will know if the error really occured or not?Using something like event.getResults().contains("error") doesn't seem to me as a correct solution. 在我的Servlet中,但是客户端如何知道错误是否确实发生?在我看来,使用诸如event.getResults().contains("error")似乎并不正确。

So I am thinking about using RequestBuilder but I don't see a way how could I get the the uploaded file and push it to my servlet. 因此,我正在考虑使用RequestBuilder但没有看到如何获取上载的文件并将其推送到Servlet的方法。 Or maybe converting my message to JSON would help? 或者将我的消息转换为JSON会有所帮助?

You should refer to this thread on the google gwt discussion group. 您应该在google gwt讨论组上参考此主题 The way you described, parsing the event.getResults() to determine if there was an error or the result in case of a success is the correct way to do it, even though it might seem barbaric. 您描述的方法是解析event.getResults()以确定成功时是否存在错误或结果,尽管这样做似乎很野蛮,但却是正确的方法。

As suggested in the linked discussion, you can look into GWT Upload for cleaner code, as well as upload progress information. 如链接讨论中所建议,您可以查看GWT Upload以获取更干净的代码,以及上载进度信息。 I believe your only two options to upload files to a server from a web page are forms or Flash. 我相信从网页上载文件到服务器的唯一两个选择是表格或Flash。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM