i use Springboot for backend and database, front as react.
i want to use saveUrl in kendo-upload ui post my image to springboot and saving image in mysql db.
i have "uploadFile" variable as axios request parameter
@RestController
@CrossOrigin
public class MemberController {
@Autowired
MemberMapper mapper;
MultipartFile upload;
String photoname;
@PostMapping(value = "/member/upload", consumes = {"multipart/form-data"})
public Map<String, String> fileUpload(@RequestParam MultipartFile uploadFile, HttpServletRequest request){
String uploadPath = request.getSession().getServletContext().getRealPath("/WEB-INF/photo");
System.out.println(uploadPath);
int pos = uploadFile.getOriginalFilename().lastIndexOf(".");
String ext = uploadFile.getOriginalFilename().substring(pos);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
photoname = "jeju" + sdf.format(date) + ext;
upload = uploadFile;
Map<String, String> map = new HashMap<String, String>();
map.put("photoname", photoname);
return map;
}
and
for react
let uploadFile = null;
imageUpload=(e)=>{
const uploadFile = e.affectedFiles[0].name;
const memberFile = new FormData();
memberFile.append("uploadFile",uploadFile);
axios({
method: 'post',
url: URL + '/member/upload',
data: memberFile,
headers: {'Content-Type':'multipart/form-data'}
}).then(response=>{
alert(response.data.photoname+" ==> save as this name");
this.setState({
photoname: response.data.photoname
})
}).catch(err=>{
console.log("error while uploading images:"+err);
})
}
onAdd = (event) => {
const afterStateChange = () => {
event.affectedFiles
.filter(file => !file.validationErrors)
.forEach(file => {
const reader = new FileReader();
reader.onloadend = (ev) => {
this.setState({
filePreviews: {
...this.state.filePreviews,
[file.uid]: ev.target.result
}
});
};
reader.readAsDataURL(file.getRawFile());
});
};
this.setState({
files: event.newState,
events: [
...this.state.events,
`selected file: ${event.affectedFiles[0].name}`
],
}, afterStateChange);
uploadFile = event.affectedFiles[0].name;
}
render(){
return (
<div>
<InputLabel id="demo-simple-select-label">picture</InputLabel>
<br />
<Upload
batch={false}
multiple={true}
files={this.state.files}
onAdd={this.onAdd}
onRemove={this.onRemove}
onProgress={this.onProgress}
onStatusChange={this.onSatusChange}
withCredentials={false}
saveUrl={URL + '/member/upload'}
removeUrl={'https://demos.telerik.com/kendo-ui/service-v4/upload/remove'}
onChange={this.imageUpload.bind(this)}
/>
<div className={'example-config'} style={{ marginTop: 20 }}>
<ul className={'event-log'}>
{
this.state.events.map(event => <li key={event}>{event}</li>)
}
</ul>
</div>
{
this.state.files.length ?
<div className={'img-preview example-config'}>
<h3>image preview</h3>
{
Object.keys(this.state.filePreviews)
.map((fileKey, index) => (<img
src={this.state.filePreviews[fileKey]}
alt={index}
style={{ width: 200, margin: 10 }}
/>))
}
</div> : undefined
}
</div>
);
}