我一直试图将文件上传到我的rest api。但是我无法确定如何获取已发送的文件,无法在正文中找到它。
order by
我应该通过正文发送文件吗?当我要访问该请求的主体时,它的主体不包含该文件。
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Field, reduxForm} from 'redux-form';
import {bindActionCreators} from 'redux';
import {uploadFile} from "../DashboardActions";
class UploadButton extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.fileInput = React.createRef();
}
handleSubmit(event) {
event.preventDefault();
console.log(this.fileInput.current.files[0]);
this.props.uploadFile(this.fileInput.current.files[0]);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Upload file:
<input name="file" type="file" ref={this.fileInput} />
</label>
<br />
<button type="submit">Submit</button>
</form>
);
}
}
function mapStateToProps(state) {
return({
// Nothing in yet
});
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({
uploadFile
}, dispatch);
}
export default reduxForm({
form: 'UploadButton2'
})(connect(mapStateToProps, mapDispatchToProps)(UploadButton));
如何将文件传递到multer存储对象中?我无法确定如何访问文件
import axios from 'axios';
import {UPLOAD_SUCCESS, UPLOAD_FAIL} from "../../../actions";
const API_URL = 'http://localhost:8080';
export function uploadFile(file) {
console.log('Triggered upload.');
console.log('File: ', file);
return dispatch => {
const config = {
headers: {
'content-type': 'multipart/form-data'
}
};
axios.post(API_URL + '/files/upload', {
file: file
}, config
).then((response) => {
console.log('Upload success', response.data);
dispatch({type: UPLOAD_SUCCESS, payload: response.data})
}).catch((error) => {
console.log(error);
dispatch({type: UPLOAD_FAIL, payload: 'File couldn\'t be saved'})
})
};
}
当我想通过axios将数据参数发送到我的rest api时,file参数包含数据,但是一旦进入rest api,我就无法确定文件数据的去向。寻求帮助!