React code for build jsonBlob object
JavaScript
x
22
22
1
function jsonBlob(obj) {
2
return new Blob([JSON.stringify(obj)], {
3
type: "application/json",
4
});
5
}
6
7
exportFTP = () => {
8
const formData = new FormData();
9
formData.append("file", jsonBlob(this.state.ipData));
10
alert("Logs export to FTP server")
11
12
axios({
13
method: "post",
14
url: "http://localhost:8080/api/auth/uploadfiles",
15
data: formData,
16
headers: {
17
Accept: "application/json ,text/plain, */*",
18
"Content-Type": "multipart/form-data",
19
},
20
});
21
};
22
Spring boot backend that accepts for frontend request
JavaScript
1
19
19
1
public class UploadFile {
2
@Autowired
3
private FTPClient con;
4
5
@PostMapping("/api/auth/uploadfiles")
6
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
7
8
try {
9
boolean result = con.storeFile(file.getOriginalFilename(), file.getInputStream());
10
System.out.println(result);
11
12
} catch (Exception e) {
13
System.out.println("File store failed");
14
}
15
16
return "redirect:/";
17
}
18
19
I want to figure out when I called the function from the frontend it’s working properly but I change the state its doesn’t send the object to the backend while the file appears in the directory. if I delete the file then only send it again and save it on the directory. How I save multiple files while doesn’t delete the previous ones
Thank you very much for your time and effort.
Advertisement
Answer
In React application I used props to pass the file name from a different state and make sure to remove,
JavaScript
1
2
1
"Content-Type": "multipart/form-data",
2
Main function in React,
JavaScript
1
16
16
1
exportFTP = ({props from different state}) => {
2
const formData = new FormData();
3
formData.append("file", jsonBlob(this.state.ipData),{You can use this parm for pass name});
4
alert("Logs export to FTP server")
5
6
axios({
7
method: "post",
8
url: "http://localhost:8080/api/auth/uploadfiles",
9
data: formData,
10
headers: {
11
Accept: "application/json ,text/plain, */*"
12
13
},
14
});
15
};
16
And back end code I used same to get the original name then Its appears with the right name.
JavaScript
1
2
1
con.storeFile(file.getOriginalFilename(), file.getInputStream());
2
Chears !!