I have a SharePoint list called complex form, which has the following columns Title, FirstName, LastName, About, Country, State, Street, City, Zip, Email, Company, Photo, the photo is an image column this is the method I am using to make the post request: no image is uploaded after making the request after inspecting the network tab the photo is shown as this- Photo:{}
private AddContact = async (event: any) => {
event.preventDefault();
const {
Title,
FirstName,
LastName,
About,
Country,
State,
Street,
City,
Zip,
Email,
Photo,
Role,
Company,
} = this.state;
const assests = await sp.web.lists.ensureSiteAssetsLibrary();
const fileItem = await assests.rootFolder.files.add(
Photo.name,
Photo,
true
);
const img = {
ServerRelativeUrl: fileItem.data.ServerRelativeurl,
};
const listName = "ComplexForm";
try {
const response: ItemAddResult = await sp.web.lists
.getByTitle(listName)
.items.add({
Title: Title,
FirstName: FirstName,
LastName: LastName,
About: About,
Country: Country,
State: State,
Street: Street,
City: City,
Photo: JSON.stringify(img),
Zip: Zip,
Email: Email,
Company: Company,
Role: Role,
});
if (response.data) {
const result = await response.data;
console.log("Item created:", result);
toast.success("Request created");
this.setState((prevState) => ({
...prevState,
Title: "",
FirstName: "",
LastName: "",
About: "",
Country: "",
State: "",
Street: "",
City: "",
Photo: null,
Zip: "",
Email: "",
Company: "",
Role: "",
isModalOpen: false,
}));
} else {
console.error("Error missing response data");
toast.error("Unable to create contact");
}
} catch (error) {
console.error("Error creating item:", error);
}
};