超过 FileSizeMax 时取消 FileUpload [英] Cancel FileUpload when FileSizeMax is exceeded

查看:37
本文介绍了超过 FileSizeMax 时取消 FileUpload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 JBoss 6.1 中运行的 JSF 应用程序,它使用内部Tomcat Servlet 容器.

I have a JSF application which runs in JBoss 6.1 which uses internal the Tomcat Servlet container.

我已经通过 apache commons 文件上传实现了上传.我想防止上传过大的文件并设置了属性 FileUploadBase 类中的 fileSizeMax 为 10MB.它有效,文件对于所有大于10MB.此异常会在不到一秒的时间内引发.但主要问题是,整个文件将通过网络.我通过检查网络流量发现了这一点.之后重定向到错误页面完成.

I've realised the upload with apache commons file upload. I want to prevent too large file uploads and have set the property fileSizeMax to 10MB within the class FileUploadBase. It works, the file upload throws an FileSizeLimitExceededException for all files larger than 10MB. This exception throws within less than a second. But the main problem is, that the whole file will be transferred over the network. I have found this out by checking the network traffic. Afterwards the redirect to the error page is done.

超过最大大小时如何中断文件传输不传输整个文件?我假设该文件将是由于 Web 表单属性 enctype,在多个包中传输="multipart/form-data".

How can I interrupt the file transfer when the max size is exceeded without transferring the whole file? I assume that the file will be transferred in multiple packages because of the web form attribute enctype ="multipart/form-data".

推荐答案

您不能中止 HTTP 请求.如果这样做,您将无法返回 HTTP 响应,并且客户端最终将得不到任何形式的反馈,预计可能会出现特定于浏览器的对等连接重置"错误页面.

You cannot abort a HTTP request halfway. If you did it, you would not be able to return a HTTP response and the client would end up with no form of feedback, expect maybe a browser-specific "Connection reset by peer" error page.

最好的办法是事先在 JavaScript 中验证它.顺便说一下,这只适用于 支持 HTML5 File API 的浏览器.您没有说明您使用的是哪个 JSF 文件上传组件,所以我的印象是您只是自制了一个,所以我将给出一个适用于呈现的 HTML <input 类型的通用答案="file">(请注意,它在例如 战斧的):

Your best bet is to validate it in JavaScript beforehand. This works by the way only in browsers supporting HTML5 File API. You didn't tell anything about which JSF file upload component you're using, so I have the impression that you just homebrewed one, so I'll give a generic answer which is applicable on the rendered HTML <input type="file"> (note that it works as good on e.g. Tomahawk's <t:inputFileUpload>):

<input type="file" ... onchange="checkFileSize(this)" />

像这样

function checkFileSize(inputFile) {
    var max = 10 * 1024 * 1024; // 10MB

    if (inputFile.files && inputFile.files[0].size > max) {
        alert("File too large."); // Do your thing to handle the error.
        inputFile.value = null; // Clears the field.
    }
}

如果旧版浏览器不支持此功能,那么您就输了.最好的选择是 Flash 或 Applet.

In case of older browsers not supporting this, well, you're lost. Your best alternative is Flash or Applet.

这篇关于超过 FileSizeMax 时取消 FileUpload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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