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

查看:30
本文介绍了使用 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.

推荐答案

只需添加原生方法:

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

然后这样称呼它:

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

对我来说效果很好.

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

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