2012-06-28 3 views
1

jQuery를 사용하여 파일을 업로드하고 서버의 폴더에 저장하려고합니다. 나는 어떻게 시작 해야할지 모르겠다. 파일 경로도 Oracle 데이터베이스에 저장해야합니다. 내 전체 시나리오는 Entity Framework를 기반으로합니다. 누구든지이 문제를 해결하는 방법에 대한 아이디어가 있습니까?jquery를 사용하여 파일을 업로드하는 방법은 무엇입니까?

+0

방문 http://stackoverflow.com/questions/4668086/ 모든 플러그인 업로드 파일 [1] : –

답변

0

-에서 좀 더 괜찮은 플러그인을 찾아

http://blueimp.github.com/jQuery-File-Upload/

을 OR -

http://www.uploadify.com/

OR, 이것을 사용 jquery에서 플러그인을 사용하고 싶지 않은 경우. 나는 여러 날 동안 그 문제에 직면했다. 이제 나는 그것을 해결했다.

$(document).ready(function() { 
    $("#formsubmit").click(function() {  

     var iframe = $('<iframe name="postframe" id="postframe" class="hidden" src="about:none" />');   

     $('div#iframe').append(iframe);   

     $('#theuploadform').attr("action", "/ajax/user.asmx/Upload") 
     $('#theuploadform').attr("method", "post") 
     $('#theuploadform').attr("userfile", $('#userfile').val()) 
     $('#theuploadform').attr("enctype", "multipart/form-data") 
     $('#theuploadform').attr("encoding", "multipart/form-data") 
     $('#theuploadform').attr("target", "postframe")   
     $('#theuploadform').submit(); 
     //need to get contents of the iframe 
     $("#postframe").load(
      function() { 
       iframeContents = $("iframe")[0].contentDocument.body.innerHTML; 
       $("div#textarea").html(iframeContents); 
      } 
     ); 



<div id="uploadform"> 
    <form id="theuploadform" action=""> 
     <input id="userfile" name="userfile" size="50" type="file" /> 
     <input id="formsubmit" type="submit" value="Send File" /> 
    </form> 
</div> 

<div id="iframe" style="width: 0px; height: 0px; display: none;"> 
</div> 

<div id="textarea"> 
</div> 

파일을 업로드합니다. 이제 남은 것은 서버에서 파일을받는 것뿐입니다. 저는 서블릿을 사용하여 파일을 얻었고 그 후에 하나의 서비스를 호출했습니다. 그 예는 모든 파일의 이미지, 텍스트, DOCX, 문서에 대한 작동 등

서블릿 :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
try { 
    ServletFileUpload upload = new ServletFileUpload(); 
    response.setContentType("text/plain"); 
    //response.setContentType("application/msword"); 


    FileItemIterator iterator = upload.getItemIterator(request); 

    while (iterator.hasNext()) { 
     FileItemStream item = iterator.next(); 
     String filename = item.getName(); 
     InputStream stream = item.openStream(); 
     //more here you wanna to do with that file do. 
    } 
} 
catch (FileUploadException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

즐기 ...

관련 문제