2013-07-13 3 views
1

파일을 만들 때 CouchDB 문서에 첨부하려고합니다. 난CouchDB, Kanso, 파일 업로드 오류

{"error":"doc_validation","reason":"Bad special document member: _attachments"} 

:

<form> 
    <input type="hidden" name="type" value="devtest"/> 
    <input type="file" name="_attachments" id="picture" accept="image/*" multiple/> 
    <button type="submit">Go</button> 
</form> 

은 JS 코드는 업로드

$(document).on('submit', 'form', function(e) { 

    e.preventDefault(); 

    var formdata = $(this).serializeObject(); // Provided by a pluign 

    $(this).find(':file').each(function(k,v) { 
    formdata[$(this).attr('name')] = $(this).val(); // Treating files 
    }); 

    // Using the kanso db package 
    m.db.current().saveDoc(
    formdata, 
    function(err,res) { 
     console.log('gna'); 
    }); 

}); 

이 다음과 같은 오류 (500) 메시지를 생성을 처리 :

는 HTML 코드 kanso 0.2.2 및 db 0.1.0과 함께 CouchDB 1.3.1을 사용합니다.

답변

1

알았어. 알아 냈어. 나의 두 가지 가정은 근본적으로 잘못된 것으로 판명되었습니다. 첫째, _attachments이라는 입력 필드 만 사용하여 첨부 파일을 업로드 할 수 없습니다. 둘째, CouchDB는 첨부되어야하는 문서가 이미 존재하지 않는 한 파일을 받아들이지 않는 것처럼 보입니다. (나는이 일에 잘못 될 수 있지만 나를 위해 작동하지 않습니다.) 여기

나를 위해 작동하는 솔루션입니다 :

    :

    $(document).on('submit', '#userctl form', function(e) { 
    
        e.preventDefault() 
        var data = $(this).serializeObject(); 
    
        // Use the Kanso API to create a normal document without attachments 
        m.db.current().saveDoc(
        data, 
        function(err,res) { 
    
         // Use ajaxSubmit provided by the jquery.forms plugin to submit the attachments 
         $('#userctl form').ajaxSubmit({ 
         url: m.db.current().url + '/' + res.id, 
         data: { 
          _rev: res.rev // Provide a revision, otherwise the upload fails 
         }, 
         success: function(res) { 
          console.log(res); 
         } 
         }); 
    
        } 
    ); 
    }); 
    

    이러한 접근 방식은 다음과 같은 두 가지 플러그인이 필요

  • jquery.form
  • jquery.serializeObject