`Success: 'Link Data Fetch Successfully', AllProjectsF23: 'Age and Gender Detection Using Deep Learning, Abst…on, Functional Requirements:, Tools:, Supervisor:'}
AllProjectsF23
:
"Age and Gender Detection Using Deep Learning, Abs
Success
:
"Link Data Fetch Successfully"`
I have above api response where AllProjectsF23 is dynamic file name and depend on input file name. I use axios post method to request django backend. My api code is:
`
const handleUpload = async () => {
try {
if (selectedFile) {
const formData = new FormData();
formData.append("files", selectedFile);
const response = await axios.post(
"xxxxxxxxxxxxxxxxxxxxxxx",
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
}
);
console.log("Server Response:", response.data);
setResponseData(response.data);
console.log(responseData);
console.log("Success:");
} else {
console.error("No file selected");
}
} catch (error) {
console.error("Error uploading file:", error);
}
};
how i do this easily and map this response to textbox in reactjs
2
Answers
To dynamically handle the response and map it to a textarea in a React component, you can follow this:
first, Update State for File Content:
Initialize a state variable to store the dynamic file content received from the API response. like this
the you need to update your FromData Key .
Assuming that the dynamic file name is available in the response, update the key in the formData.append method to use the dynamic file name.
Make sure to replace ‘AllProjectsF23’ with the actual dynamic key you want to use from the API response. Adjust the code according to your specific requirements. With these changes, the file content should be displayed in the textarea in your React component.
yea! To make the header flexible and handle different dynamic file names, you can use the following approach
This code assumes that the API response has only one dynamic key, and it uses Object.keys to get that key dynamically. Adjust the code according to your specific API response structure.
If you have multiple dynamic keys in your API response, you might need to loop through them or use a specific key based on your requirements.
For example, if your API response looks like this:
You can modify the code to loop through all keys and display them in a dropdown or handle them in another way:
Then, you can provide an option for users to select the dynamic key they want to display in the text area.