Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React中上载图像和其他文本输入?_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 在React中上载图像和其他文本输入?

Javascript 在React中上载图像和其他文本输入?,javascript,reactjs,redux,Javascript,Reactjs,Redux,非常奇怪的是,我在网上找到的所有教程都显示了如何上传图像,但没有显示如何在包含其他文本输入的情况下进行上传 因此,我在尝试上传图像以及相同形式的其他文本数据时遇到了障碍。花了几个小时在SO和Google上搜索,但找不到任何适合我的情况 我正在使用React&Redux和expressfileupload包进行文件上传 不管怎样,以下是我尝试过的: 后端 campgroundRoutes.js // Only including add campground action here as only

非常奇怪的是,我在网上找到的所有教程都显示了如何上传图像,但没有显示如何在包含其他文本输入的情况下进行上传

因此,我在尝试上传图像以及相同形式的其他文本数据时遇到了障碍。花了几个小时在SO和Google上搜索,但找不到任何适合我的情况

我正在使用React&Redux和expressfileupload包进行文件上传

不管怎样,以下是我尝试过的:

后端

campgroundRoutes.js

// Only including add campground action here as only that's relevant

import {
  ADD_CAMPGROUND,
} from '../actionTypes/types';

import axios from 'axios';
import { authHeader } from '../../../helpers/auth-header';

const API_URL = `http://localhost:5000/api`;

export const actionAddCampground = campground => {
  return async dispatch => {
    try {
      const res = await axios.post(`${API_URL}/campgrounds`, campground, {
        headers: authHeader()
      });
      dispatch({
        type: ADD_CAMPGROUND,
        payload: res
      });
      return res.data;
    } catch (err) {
      return console.log(err);
    }
  };
}; 
前端

addCampground.js

// Only including add campground action here as only that's relevant

import {
  ADD_CAMPGROUND,
} from '../actionTypes/types';

import axios from 'axios';
import { authHeader } from '../../../helpers/auth-header';

const API_URL = `http://localhost:5000/api`;

export const actionAddCampground = campground => {
  return async dispatch => {
    try {
      const res = await axios.post(`${API_URL}/campgrounds`, campground, {
        headers: authHeader()
      });
      dispatch({
        type: ADD_CAMPGROUND,
        payload: res
      });
      return res.data;
    } catch (err) {
      return console.log(err);
    }
  };
}; 
authHeader.js

// Only including add campground action here as only that's relevant

import {
  ADD_CAMPGROUND,
} from '../actionTypes/types';

import axios from 'axios';
import { authHeader } from '../../../helpers/auth-header';

const API_URL = `http://localhost:5000/api`;

export const actionAddCampground = campground => {
  return async dispatch => {
    try {
      const res = await axios.post(`${API_URL}/campgrounds`, campground, {
        headers: authHeader()
      });
      dispatch({
        type: ADD_CAMPGROUND,
        payload: res
      });
      return res.data;
    } catch (err) {
      return console.log(err);
    }
  };
}; 
这是我在后端遇到的错误:

TypeError: Cannot read property 'file' of undefined

我不知道哪里出了问题。

几天前,我甚至面临着同样的问题,我必须发送文件和另一个输入字段。最后,下面的代码在进行API调用时帮助了我,看看是否对您有帮助

//-------------To insert Documents and InputField--------------//

export function documentsInsert( documentName, document ) {

var formData = new FormData();
formData.append("documentName", documentName)
formData.append("document", document)

return request2({
    url: xyzzz,
    method: 'POST',
    body: formData
});
}
//-------------To insert Documents and InputField--------------//

export function documentsInsert( documentName, document ) {

var formData = new FormData();
formData.append("documentName", documentName)
formData.append("document", document)

return request2({
    url: xyzzz,
    method: 'POST',
    body: formData
});
}