js中获取文件的大小,判断文件是否操作大小限制,避免到后台报错。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<
head
>
<
title
>Get File Size</
title
>
<
script
src
=
"http://code.jquery.com/jquery-1.11.0.min.js"
type
=
"text/javascript"
>
</
script
>
<
script
type
=
"text/javascript"
>
//判断文件大小,传入id
function GetFileSize(fileid) {
try {
var fileSize = 0;
//for IE
if ($.support.msie) {
//before making an object of ActiveXObject,
//please make sure ActiveX is enabled in your IE browser
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
fileSize = fileSize / 1048576; //size in mb
}
//for FF, Safari, Opeara and Others
else {
fileSize = $("#" + fileid)[0].files[0].size //size in kb
fileSize = fileSize / 1048576; //size in mb
}
alert("Uploaded File Size is" + fileSize + "MB");
} catch (e) {
alert("Error is :" + e);
}
}
</
script
>
</
head
>
<
body
>
<
form
name
=
"upload"
action
=
""
>
<
input
type
=
"file"
name
=
"fUpload"
id
=
"fUpload"
/> <
input
type
=
"button"
value
=
"Get File Size"
onclick
=
"GetFileSize('fUpload');"
/>
</
form
>
</
body
>
|
本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/1969952,如需转载请自行联系原作者