Get the uploading file size without activexobject?
Is it possible to get the file size of an uploading file using java script or without activex object ? It sho开发者_运维问答uld be compatible with all browsers.
Compatible for all browsers? - no solution
Compatible with firefox,chrome,safari,opera ?- following script
document.getElementById("file_elem_id").files[0].size
Doesnt works with IE8(yet to test on IE9)
Let you know that jQuery files API is not supported for IE-9 or lower versions. In these cases we are bound to check file size by creating the ActiveXObject as this is the only way for that case to deal with system files to get the file size before upload.
But here another issue is there like ActiveX needs to be installed/enabled/configured properly on browser on client machine otherwise it will not be recognized. Therefore i have three ways to get file size here :- 1) Get the file size by transferring the whole file to server and return the file size from there. 2) It is very easily can be found by using the Silverlight, Flash or such things on client-side. 3) Upload the file in chunks to server when it crosses the file size limit then return user with a message.
Get file size in jQuery(supported in IE 10, FF, Chrome browsers)
$('#fileInputID').change(function(){
var file=this.files[0];
alert(file.size||file.fileSize);
})
Get file size in using ajax, making HEAD request only :
var response = $.ajax({
type: "HEAD",
url: fileurl,// file url on server
success: function () {
alert("Size of file is " + request.getResponseHeader("Content-Length"));
}
});
Hope this will be helpful.
References :- http://www.aspnetcodes.com/2012/07/example-to-get-file-size-before-upload.html
精彩评论