2011-03-11 3 views
3

업로드하기 전에 크기를 확인한 Twitter에 프로필 사진을 업로드 할 때주의를 받았습니다. 아무도이 같은 솔루션을 날 가리킬 수 있습니까?업로드 전의 이미지 크기 확인

감사

답변

2

당신은 파일 크기가 업로드하기 전에 파일 크기를 확인하는 uploadify 같은 것을 사용할 수 있습니다 확인하는 경우.

실제 파일 크기 (높이/너비)를 확인하려면 서버에서 수행해야 할 수 있습니다.

1

플래시를 사용하지 않으면 불가능하다고 생각합니다. 그런 경우에는 uploadify 또는 swfupload을 사용할 수 있습니다.

2

크롬에서이 코드를 테스트했으며 정상적으로 작동합니다. 이 같은

var x = document.getElementById('APP_LOGO'); // get the file input element in your form 
var f = x.files.item(0); // get only the first file from the list of files 
var filesize = f.size; 
alert("the size of the image is : "+filesize+" bytes"); 

var i = new Image(); 
var reader = new FileReader(); 
reader.readAsDataURL(f); 
i.src=reader.result; 
var imageWidth = i.width; 
var imageHeight = i.height; 
alert("the width of the image is : "+imageWidth); 
alert("the height of the image is : "+imageHeight); 
0

뭔가 또는 "복수"없이 비동기 적으로로드 형태의 입력에 대처해야 설정하고 FileReader.readAsDataURL 및 Image.src를 사용할 때 일어나는 경쟁 조건을 피하십시오.

$('#formContainer').on('change', '#inputFileUpload', function(event) { 
    var file, _fn, _i, _len, _ref; 
    _ref = event.target.files; 
    _fn = function(file) { 
    var reader = new FileReader(); 
    reader.onload = (function(f) { 
     return function() { 
     var i = new Image(); 
     i.onload = (function(e) { 
      var height, width; 
      width = e.target.width; 
      height = e.target.height; 
      return doSomethingWith(width, height); 
     }); 
     return i.src = reader.result; 
     }; 
    })(file); 
    return reader.readAsDataURL(file); 
    }; 
    for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
    file = _ref[_i]; 
    _fn(file); 
    } 
}); 

참고 :

<div id="formContainer"> 
    <form ...> 
    ... 
    <input type="file" id="inputFileUpload"></input> 
    ... 
    </form> 
</div> 
0

당신은 이미지 너비와 높이 이미지 온로드를 확인해야합니다 같은 구조에 jQuery를, HTML5, 후크를 필요로한다.

var img = new Image; 
img.src = URL.createObjectURL(file); 
img.onload = function() { 
    var picWidth = this.width; 
    var picHeight = this.height; 

    if (Number(picWidth) > maxwidth || Number(picHeight) > maxheight) { 
     alert("Maximum dimension of the image to be 1024px by 768px"); 
     return false; 
    } 
}