如何将文件作为附件上传到CouchDB实例?

时间:2012-08-28 03:47:47

标签: html couchdb

我有一个HTML表单,我可以用它来创建当前只包含文本内容的文档。但是,我希望能够通过表单上传文件数据。

我该怎么做?这是我目前使用的表格

<form class="form-horizontal well" id="submitform" name="submitform">
  <label class="control-label" for="brand">Brand</label>
          <label class="control-label" for="link">Any other thoughts?</label>
          <div class="control-group">
            <div class="controls">
              <textarea class="input" rows="3" name="notes" id="notes"></textarea>
            </div>
          </div>

          <div class="control-group">
            <div class="controls">
              <input type="submit" class="btn"/>
            </div>
          </div>

          <label class="control-label" for="picture[]">Got a Picture?</label>
          <div class="control-group">
            <div class="controls">
              <input type="file" name="_attachments" id="picture" accept="image/*" multiple/>
            </div>
          </div>
          </div>
      </fieldset>
    </form>

我尝试使用一些AJAX上传文件,但我只能将文件名作为附件上传,而不是实际内容。

$.couch.db("couchclothes").saveDoc
  (
    {
      type:   $('#type').val(), 
      brand:  $('#brand').val(), 
      fit:    $('#fit').val(),
      color:  $('#color').val(),
      link:   $('#link').val(),
      notes:  $('#notes').val()
    },  
    {
      success: function(data) 
      { 
        alert("Saved data ok."); 
        var id = data.id;
        var rev = data.rev;

        // send a PUT request here
      }
    }  
  );  

1 个答案:

答案 0 :(得分:1)

我也对如何使用字段和文件进行经典表单POST感兴趣。

但是,如果可以通过两个单独的步骤进行数据和文件上传(并回答您的问题),请选中适用于XHR2浏览器的解决方案:How to upload a file (attachment) from the browser?