使用GWT上传之前检查文件大小 [英] Check file size before upload using GWT

查看:124
本文介绍了使用GWT上传之前检查文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FormPanel将文件上传到服务器。我想要做的是在上传文件之前确保文件大小小于给定大小(例如10MB)。
在服务器端,我有一个javax.servlet.http.HttpServlet,在其中我重写了doPost。这里的HttpServletRequest参数具有文件的大小,但问题是我没有达到doPost,直到文件已经上传。因此,如果我选择上传1 GB文件,那么在整个文件上传后,客户端收到的文件太大错误消息不会显示30秒左右。

I'm using a FormPanel to upload a file to a server. What I'm trying to do is make sure the file size is smaller than a given size (e.g 10MB) before uploading it. On the server side I have a javax.servlet.http.HttpServlet where I'm overriding doPost. Here the HttpServletRequest parameter has the size of the file, but the problem is I don't reach doPost until the file has already been uploaded. So if I chose to upload a 1 GB file, the "File too large" error message the client receives doesn't show up for 30 seconds or so, after the entire file has been uploaded.

在上传之前,没有办法在客户端检查大小吗?或者,如果数据大小超过特定大小,则让servlet抛出异常。

Isn't there any way to check the size on the client side, before it's uploaded? Alternatively, get the servlet to throw an exception if the size of the data exceeds the specific size.

推荐答案

只需添加本地方法即可:

Just add native method:

private native int getFileSize(final Element data) /*-{
            return data.files[0].size;
        }-*/;

然后像这样调用它:

And then call it like this:

final FileUpload upload = new FileUpload();
//
// some actions
//
final int size = getFileSize(upload.getElement())

对我来说工作得很好。

这篇关于使用GWT上传之前检查文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆