2012-12-17 2 views
0

가능한 중복 :
In JavaScript can I make a “click” event fire programmatically for a file input element?은 iframe에서 자바 스크립트 클릭 요소

내가 번의 클릭으로 업로드를하고 싶었

그래서 난 이런 <iframe> 내부 <form>가 (난 그냥 <form> 주위 <iframe> 태그를 입력 숨기기 양식 숨기기) :

<iframe id="iframe" name="iframe" height="1" width="1" frameborder="0"> 
    <form id="form1" enctype="multipart/form-data" method="post" action="uploaded.php"> 
     <div class="row"> 
     <label for="file">Select a File to Upload (up to 20MB)</label><br /> 
     <input type="file" name="file" id="file" size="40" onchange="uploadFile()" /> 
     </div> 
    </form> 
</iframe> 

<div align="center"> 
    <input type="button" value="Upload" align="center" onclick="browse();" /> 
</div> 

찾아() 자바 스크립트는 이것이다 :

그런 다음 <iframe> 외부 나는 버튼을 만들어

function browse() 
{ 
    var iframe = document.getElementById.("iframe"); 
    var input = iframe.contentDocument || iframe.contentWindow.document; 
    input.getElementById('file').click(); 
} 

을하지만 작동하지 않습니다. 찾아보기 파일 대화 상자가 열리지 않습니다. Firefox 17을 사용하고 있습니다. 또는 내의 <input> 또는 다른 모든 원 클릭 업로드 솔루션을 클릭() 할 수 있나요? 아직 내 uploadFile() AJAX 기능을 사용하고 있습니까?

function uploadFile() { 
     var fd = new FormData(); 
     fd.append("file", document.getElementById('file').files[0]); 
     var xhr = new XMLHttpRequest(); 
     xhr.upload.addEventListener("progress", uploadProgress, false); 
     xhr.addEventListener("load", uploadComplete, false); 
     xhr.addEventListener("error", uploadFailed, false); 
     xhr.addEventListener("abort", uploadCanceled, false); 
     xhr.open("POST", "uploaded.php"); 
     xhr.send(fd); 
     } 

편집 : 폼이 iframe에없는 경우이 작동하고, 그래서 제 질문은하지 처리 여부 여부를 나는 <input type="file">에 클릭()를 사용할 수있다. 나는 파이어 폭스 17에서 일하고있다. 내 질문은 <input type="file"> 안에 에있는 click()을 사용하는 방법이다. 브라우저가 iframe을 지원하지 않는 경우에 사용하는 다른 내용은 유형이 있기 때문에

+0

동일한 도메인에 iframe이 있습니까? – epascarello

+0

iframe 태그를 index.php 페이지 안의 양식에 넣기 만하면됩니다. 동일한 파일, 동일한 도메인, 외부 소스 없음 – cikatomo

+0

왜 iframe을 그렇게 사용 하시겠습니까? – epascarello

답변

0

자바 스크립트가 추가 .

var iframe = document.getElementById.("iframe"); 
            ^-- typo 
+0

네, 교정 해 보았지만 여전히 작동하지 않습니다. – cikatomo

0

<iframe>의 자식 요소로 오류를 던지는해야합니다. 그들은 프레임에로드 된 문서의 일부를 구성하지 않습니다 (이는 contentDocument에 액세스 할 것입니다).

문제 (또는 적어도 첫 번째 문제)는 콘텐츠를 숨기기 위해 해킹으로 사용하려고한다는 것입니다. 그러지 마.