그리고 다 만들고 뿌듯해하고 있는데 내장함수를 발견했다는...
역시 배워야한다는... 흑 _ㅠ)...
출처 - 자작
// 파일의 이름을 구하는 함수
// 마지막 \의 자리를 구한 후 파일명만을 추출한다.
function getFileName(path) {
var intIndex;
var intIndexCount;
var strFileName;
for(var i=1 ; i<path.length+1 ; i++) {
intIndex = path.substring(i-1, i);
if(intIndex == "\\") intIndexCount = i;
}
strFileName = path.substring(intIndexCount, path.length);
return strFileName;
}
// 파일의 용량을 구하는 함수
function getFileSize(path)
{
var img = new Image();
var strSize;
var intSize;
var strResult;
img.dynsrc = path;
intSize = img.fileSize;
strResult = getFileSizeFormat(intSize);
// file 태그를 지울 때 getFileSize(param1) 함수가 한번 실행되고 임의의 값인 4가 출력되는 버그가 발생
// path == "" 조건를 주어 4 Bytes 파일을 업로드 할 때 용량이 표시가 안 되는 것을 방지.
if ((path == "") && (strResult == 4)) strResult = "";
return strResult;
}
// 파일 용량를 형식화 하는 함수
function getFileSizeFormat(fileSize) {
var strFileSize
var intFileSizeLength; //길이
var intQuotient; //몫
var intRest; //나머지
var i, temp;
var strResult;
//파라미터 fileSize가 정수형이기 때문에 문자형으로 변환
strFileSize = fileSize.toString();
// 변환 된 strFileSize의 길이를 구한다.
// intFileSizeLength는 ,(콤마)를 찍기 위한 자리 계산에 사용된다.
intFileSizeLength = strFileSize.length;
// 길이를 3으로 나눈 몫을 구해서 ,(콤마)의 첫번째 위치를 구한다.
// intRest가 0이면 xxx,xxx 형식이고,
// 1이면 x,xxx,xxx형식이고
// 2이면 xx.xxx.xxx형식이다.
intRest = intFileSizeLength % 3;
// intRest를 이용하여 ,(콤마)의 갯수를 구한다.
if (intRest == 0) {
intQuotient = parseInt(intFileSizeLength / 3);
} else {
intQuotient = parseInt((intFileSizeLength / 3)+1);
}
for (i=0 ; i<intQuotient ; i++) {
if (intQuotient == 1) break; // 3자리 일때는 ,(콤마) 표시를 생략한다. ex.) 123 bytes
// 맨 앞의 숫자의 갯수가 3개아 아닌 경우 ex.) 12,123 bytes, 5,123,879 bytes
if ((intRest != 0) && (i == 0))
strFileSize = strFileSize.substring(0, intRest)+","+strFileSize.substring(intRest, strFileSize.length);
// 맨 앞의 숫자의 갯수가 3개인 경우 ex.) 123,123 bytes, 456,789,785 bytes
else if ((intRest == 0) && (i == 0))
strFileSize = strFileSize.substring(0, 3)+","+strFileSize.substring(3, strFileSize.length);
// 맨 앞의 숫자의 갯수를 제외한 나머지 숫자의 3자리를 끊는다.
else {
if (i == intQuotient-1) break;
if (intRest != 0) {
temp = (i*3)+intRest+i;
strFileSize = strFileSize.substring(0, temp)+","+strFileSize.substring(temp, strFileSize.length);
}
else {
temp = i*3+3+1;
strFileSize = strFileSize.substring(0,temp)+","+strFileSize.substring(temp, strFileSize.length);
}
}
}
strResult = strFileSize;
document.registForm.file1.value="";
return strResult;
}
// 표시된 숫자를 삭제
function delete_text(num) {
if (num == 1) {
document.registForm.filename1.value = "";
document.registForm.sizebox1.value = "";
// 아래 두 줄은 file 태그를 지우는 로직이다. ex.) 업로드 태그 <input type="file" name="...">
registForm.file1.select();
document.execCommand('Delete');
} else if (num == 2){
document.registForm.filename2.value = "";
document.registForm.sizebox2.value = "";
// 아래 두 줄은 file 태그를 지우는 로직이다. ex.) 업로드 태그 <input type="file" name="...">
registForm.file2.select();
document.execCommand('Delete');
}
}
'WEB 2.0 > JavaScript' 카테고리의 다른 글
| 파일 용량 표시 함수 or 화폐 3자리 콤마 찍기( FormatNumber(CCur()) ) (0) | 2008/06/22 |
|---|---|
| 팝업리사이징 스크립트 - 펌 (0) | 2008/03/20 |
| 새창(팝업) 크기 자동조절(리사이즈,resize) - 펌 (0) | 2008/03/14 |
| 자바스크립트 이벤트의 종류 - 펌 (0) | 2008/02/28 |
| javascript 함수, 메소드, 프로퍼티 (0) | 2008/01/18 |
| web browser refresh :: 펌 (0) | 2007/07/27 |
TRACKBACK 0 AND
COMMENT 0


이올린에 북마크하기
이올린에 추천하기
PREV