如何在上传之前检索文件的大小?

时间:2022-03-10 06:40:40

I have an file input tag in my web app. I'd like to check that the file isn't too big before sending it to the server. Of course, I still have validation server side. Is there any way to do this with JavaScript? It must work in IE7+ and FF3+. Thank you.

我的网络应用程序中有一个文件输入标记。我想在将文件发送到服务器之前检查文件是否太大。当然,我还有验证服务器端。有没有办法用JavaScript做到这一点?它必须在IE7 +和FF3 +中工作。谢谢。

EDIT: somefileinputobject.files[0].filesize works in FF, but not IE.

编辑:somefileinputobject.files [0] .filesize在FF中工作,但不在IE中。

5 个解决方案

#1


It's a hard problem. You have to do it with AJAX, and use the filesize headers sent by the browser to the server on the POST request.

这是一个难题。您必须使用AJAX执行此操作,并使用浏览器发送到服务器上的文件大小标头。

Yahoo's UI Library has a tool to help with this. YUI Uploader

雅虎的UI库有一个工具来帮助解决这个问题。 YUI上传器

#2


Javascript cannot do this. It would have serious security issues. Perhaps flash can though.

Javascript无法做到这一点。它会有严重的安全问题。也许闪光可以。

#3


Cannot be reliably done with Javascript for all browsers, but you can limit the total size of the posted data from the web.config

对于所有浏览器都无法使用Javascript可靠地完成,但您可以限制来自web.config的已发布数据的总大小

#4


Short answer: No, you should handle this on the server.

简短回答:不,你应该在服务器上处理这个问题。

Long answer: Not reliable. With IE you could do something like:

答案很长:不可靠。使用IE,您可以执行以下操作:

function checkSize(filespec) {
var fso, f, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile(filespec);
s = f.size;
// Do something with var s
}

But this can easily be comprimised by the browser's security settings or the usage of another browser or platform.

但这很容易通过浏览器的安全设置或其他浏览器或平台的使用来控制。

#5


You really don't have a lot of options if you are using the traditional file input control. You cannot check it on the client side and it will hit your configured maxRequestLength before you get the opportunity to check it server side. You can catch the exception that occurs when the maxRequestLength is exceeded though.

如果您使用传统的文件输入控件,那么您真的没有很多选项。您无法在客户端进行检查,它将在您有机会检查服务器端之前达到配置的maxRequestLength。您可以捕获超过maxRequestLength时发生的异常。

#1


It's a hard problem. You have to do it with AJAX, and use the filesize headers sent by the browser to the server on the POST request.

这是一个难题。您必须使用AJAX执行此操作,并使用浏览器发送到服务器上的文件大小标头。

Yahoo's UI Library has a tool to help with this. YUI Uploader

雅虎的UI库有一个工具来帮助解决这个问题。 YUI上传器

#2


Javascript cannot do this. It would have serious security issues. Perhaps flash can though.

Javascript无法做到这一点。它会有严重的安全问题。也许闪光可以。

#3


Cannot be reliably done with Javascript for all browsers, but you can limit the total size of the posted data from the web.config

对于所有浏览器都无法使用Javascript可靠地完成,但您可以限制来自web.config的已发布数据的总大小

#4


Short answer: No, you should handle this on the server.

简短回答:不,你应该在服务器上处理这个问题。

Long answer: Not reliable. With IE you could do something like:

答案很长:不可靠。使用IE,您可以执行以下操作:

function checkSize(filespec) {
var fso, f, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile(filespec);
s = f.size;
// Do something with var s
}

But this can easily be comprimised by the browser's security settings or the usage of another browser or platform.

但这很容易通过浏览器的安全设置或其他浏览器或平台的使用来控制。

#5


You really don't have a lot of options if you are using the traditional file input control. You cannot check it on the client side and it will hit your configured maxRequestLength before you get the opportunity to check it server side. You can catch the exception that occurs when the maxRequestLength is exceeded though.

如果您使用传统的文件输入控件,那么您真的没有很多选项。您无法在客户端进行检查,它将在您有机会检查服务器端之前达到配置的maxRequestLength。您可以捕获超过maxRequestLength时发生的异常。