简体   繁体   English

反应添加图像字段并保存在pouchdb中

[英]React add image field and save in pouchdb

I want to add a file/image field to my current field and save in pouchdb. 我想将文件/图像字段添加到当前字段并保存在pouchdb中。 Is there any cool way to handle this? 有什么好办法可以解决这个问题吗?

Here's what I have currently: 这是我目前所拥有的:

this.newInfo = {
      id: uuid(),
      createdAt: toDate(),
      Info: {
        name: '',
        dob: '',
        designation: ''
      }
  }

this.state = {
    doc: this.newInfo
  }

In another file: 在另一个文件中:

submitInfo (event) {
    event.preventDefault()

    let Info = Object.assign({}, this.props.doc)

    if (!Info._id) {
      Info._id = Info.id
    }

    console.log('About to post to pouch...', Info._id)

    // Save to pouchdb
    this.db.put(Info , (err, result) => {
      if (!err) {
        console.log('Successfully posted to pouchdb!')
        this.props.clearCurrentDoc()
      } else {
        console.log('Error saving to pouch...')
        console.log(err)
      }
    })
  }

The form: 表格:

<fieldset>
  <legend>Info</legend>

            <input name='name'
              value={props['name']}
              type='text'
              placeholder='Enter Name'
              onChange={handleChange}
            />
            <input name='dob'
              value={props['dob']}
              type='date'
              placeholder='Date of Birth'
              onChange={handleChange}
            />
            <input name='designation'
              value={props['designation']}
              type='text'
              placeholder='Designation'
              onChange={handleChange}
            />
          </fieldset>

I want to add one more field for saving images. 我想再添加一个字段来保存图像。 Maybe call it picture. 也许称它为图片。 Sorry, I can't put the entire code but everything else is working fine. 抱歉,我无法输入全部代码,但其他所有工作都很好。 I just need to add a field that will browse for image or file and save it with the existing ones. 我只需要添加一个字段即可浏览图像或文件,并将其与现有文件保存在一起。

I've got a good sample on jsbin that should help you to sort through this. 我在jsbin上有一个很好的示例,应该可以帮助您解决这个问题。 There is an input button that let's you select an image and then submit this to PouchDB. 有一个输入按钮,让您选择一个图像,然后将其提交到PouchDB。 If you are using CouchDB the code is also there to replicate the data to CouchDb. 如果您使用的是CouchDB,那么这里的代码也可以将数据复制到CouchDb。 The general idea is to use something like this: 一般的想法是使用这样的东西:

<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="file" name="file" />
<script>        
 var field1= window.document.reportform.field1.value;
 var field2= window.document.reportform.field2.value;
 var inputFile = document.querySelector("#file");
 getFile = inputFile.files[0];

 var myDoc = {
   _id: new Date().toISOString(),
   field1: field1,
   field2: field2,
   _attachments: {
     "file": {
       content_type: getFile.type,
       data: getFile
     }
   }
 }

 db.put(myDoc, function(err, response) {
   if (err) {
     return console.log(err);
   } else {
     console.log("Document created Successfully");
   }
});
  db.replicate.to(remote);
}
</script>

JSbin: PouchDB image select and put() JSbin: PouchDB图像选择和put()

I have not been able to get to work. 我没能上班。 I have asked on stackoverflow and the PouchDB Slack channel. 我问过stackoverflow和PouchDB Slack通道。 Some suggestions have been made but so far I haven't gotten it working. 提出了一些建议,但到目前为止,我还没有开始工作。

The PouchDB documentation is very good. PouchDB文档非常好。 PouchDB Attachments Guide PouchDB附件指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM