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

查看:257
本文介绍了超过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.它可以工作,文件 上载会为所有大于的文件抛出FileSizeLimitExceededException 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 type="file">(请注意,它是在 Tomahawk的<t:inputFileUpload> )上效果很好:

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天全站免登陆