将元数据添加到使用Axios上传的文件中。反应

时间:2019-01-07 08:06:37

标签: react-redux metadata axios iformfile

我能够使用Axios上传文档,但是无法将任何元数据添加到正在上传的文件中。我正在将文件保存到数据库中,需要将其与来自表单的数据一起保存,例如DocumentTypeID等。这是我的代码:

我的反应页面,选择文件时发生的事件:

  fileUpload(event) {
    const { dispatch } = this.props;
    var documentDetails = {
      matterID: 1,
      documentTypeID: 1
    }

dispatch(documentActions.add(documentDetails, event));

}

我的动作:

function add(documentDetails, file) {
  let data = new FormData();
  let filetoUpload = file.target.files[0];
  data.append('file', filetoUpload);

  return dispatch => {
    dispatch(request());
axios.post('/api/document/add', data, {
  headers: {
    'Content-Type': 'multipart/form-data',
    'Metadata': JSON.stringify(documentDetails)
  },
})
    .then(
  dispatch(success(function () )),
  error => dispatch(failure())
    );

};

在上面,我无法将documentDetails对象发送到我的API控制器。我只能发送文件,尝试其他操作时,发送到API的值将为空。

我的API控制器仅在发送文件时有效:

[HttpPost("add")]
        public void Add(IFormFile file)
        {
            if (file != null)
            {
                DocumentViewModel documentViewModel = new DocumentViewModel();
                Document document = new Document();

            byte[] p1 = null;
            using (var fs1 = file.OpenReadStream())
            using (var ms1 = new MemoryStream())
            {
                fs1.CopyTo(ms1);
                p1 = ms1.ToArray();
            }

            document = documentViewModel.ToDocumentEntity();
            document.DocumentType = 1;
            document.FileName = "test.txt";
            document.Name = "testing";
            document.MatterID = 1;
            document.DateUploaded = DateTime.Now;
            document.IsDeleted = false;

            using (LiquidationEntities db = new LiquidationEntities())
            {
                DocumentRepository documentRepo = new DocumentRepository();
                documentRepo.Add(db, document);
            }
        }

        var response = new
        {

        };

    }

当我尝试发送文件和对象(即视图模型)时,两者均为空:

[HttpPost("add")]
        public void Add([FromBody]DocumentViewModel documentViewModel, IFormFile file)
        {
            Document document = new Document();

        byte[] p1 = null;
        using (var fs1 = documentViewModel.FormFile.OpenReadStream())
        using (var ms1 = new MemoryStream())
        {
            fs1.CopyTo(ms1);
            p1 = ms1.ToArray();
        }

        document = documentViewModel.ToDocumentEntity();
        document.DocumentType = 1;
        document.FileName = "test.txt";
        document.Name = "testing";
        document.MatterID = 1;
        document.DateUploaded = DateTime.Now;
        document.IsDeleted = false;

        using (LiquidationEntities db = new LiquidationEntities())
        {
            DocumentRepository documentRepo = new DocumentRepository();
            documentRepo.Add(db, document);
        }

        var response = new
        {

        };

    }

如您所见,我正在对文档的详细信息进行硬编码。我如何在上传文件时发送此信息?我尝试将其添加到页眉中,但这没有用。我尝试使用我的服务而不是从操作中调用Axios,但这也不起作用,这是该服务:

function add(documentDetails, file) {
  debugger;
  let data = new FormData();
  let filetoUpload = file.target.files[0];

  const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json'},
    body: { 'documentViewModel': documentDetails, 'file': file }
  };

  var result = fetch(`${process.env.REACT_APP_API_URL}/api/document/add`, requestOptions).then(handleResponse);
  return result;
}

当我尝试同时发送视图模型和文件时,它也会失败。当我只发送文件时,它就可以工作。

任何人都可以提供帮助,让我知道如何将元数据添加到我的文件中...

0 个答案:

没有答案
相关问题