2012-09-11 8 views

답변

27

파일 업로드에는 https://github.com/blueimp/jQuery-File-Upload을 사용하는 것이 좋습니다.

+2

권장 사항. 나는 boostrap-fileupload.js의 미리보기 기능을 좋아합니다. 그래서이 js에서 evenf를 트리거하는 방법을 찾으려고했습니다. 응답을 감사하지만 jquery-file-upload에 대해 이미 알고있었습니다. –

+2

이것이 가능한지 나는 알 수 없다. jasny의 소스를 보면 jquery.fileupload에서 사용하는 jquery 함수와 동일한 이름을 사용합니다 (둘 다 $ .fileupload라고 함). 그러므로 둘을 함께 사용하는 것은 어려울 것입니다. –

6

사용자가 이미지를 탐색/선택하자마자 AJAX 요청을 통해 이미지를 업로드하고 저장된 이미지 ID로 숨겨진 필드를 업데이트하려는 비슷한 문제가 발생했습니다. bootstrap-fileupload.js로 솔루션을 찾을 수 없습니다. 그래서 아래의 접근법이 저에게 효과적이었습니다.

<script type="text/javascript" src="http://malsup.github.com/jquery.form.js"></script> 
<script type="text/javascript"> 
var options = { 
     success:  showResponse // post-submit callback 
    }; 
    $(document).ready(function() 
    { 
     $('#photoimg').live('change', function() 
     { 
      $("#imageform").ajaxForm(options).submit();   
     }); 
    }); 

    function showResponse(responseText, statusText, xhr, $form) { 
     $('#photoUrl').val(responseText); 
    } 
</script> 

이미지 양식 : (중첩 된 형태가 아니어야합니다!)

<form id="imageform" action="${pageContext.request.contextPath}/app/upload/saveimage" method="post" enctype="multipart/form-data"> 
       <div style="top: 25px"> 
       <div class="span6" style="margin-top: -545px; margin-left:680px"> 
       <div class="control-group"> 
       <label class="control-label " style="text-align: left">Photo: </label> 
       <div data-fileupload="image" class="fileupload fileupload-new"> 
        <div style="margin-left:-235px ;width: 150px; height: 150px; line-height: 150px;" class="fileupload-preview thumbnail" ></div> 
        <div> 
         <span class="btn btn-file" style="margin-right: 135px"><span class="fileupload-new" >Select image</span><span class="fileupload-exists">Change</span><input type="file" name="fileData" id="photoimg"/></span> <a data-dismiss="fileupload" class="btn fileupload-exists" href="#" style="margin-left: -75px">Remove</a> 
        </div> 
       </div> 
       </div> 

       </div> 
       </div> 
      </form> 

<input type="hidden" name="individualCustomer.personInfo.photoUrl" id="photoUrl" /> 
1

당신은 jQuery를 파일 업로드 (https://github.com/blueimp/jQuery-File-Upload)와 통합 할 수 있습니다 그것은을 가지고로 bootstrap.fileupload.js 파일을 제거하여 jQuery File Uploader와 이름이 충돌하여 올바르게 작동하지 않습니다. 이는 Jasny 컴포넌트의 고급 기능을 사용할 수 없음을 의미합니다. 하지만 코드를 읽고 자신 만의 솔루션을 구현하는 것이 좋습니다 (어렵지 않습니다 :)).

다른 방법으로는 부트 스트랩에 다른 구성 요소를 사용하는 것입니다. 이 구성 요소는 즉시 사용할 수 있으며 마크 업을 수정할 필요가 없습니다 (http://gregpike.net/demos/bootstrap-file-input/demo.html). jasny 구성 요소보다 간단하므로 일부 경우에 더 적합 할 수 있습니다.