So I'm trying to upload a file, or multiple files, to a sharepoint as attachments to a list item. I have the code to upload the actual files but they're corrupt/unopenable when trying to access them in sharepoint.
I receive these files from an outside API in the form of:
{
"File": {
"contentType": ~~~~
"fileContent": ~~~~
"name": ~~~~.docx/.pdf
}
}
I currently am trying to upload it like this:
spClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
foreach (var file in data["files"])
{
var requestString = $"{siteUrl}/lists('{listID}')/items('{siteResponseObject["id"]}')/AttachmentFiles/add(FileName='{file["File"]["name"]}')";
StringContent filePostData = new StringContent(file["File"]["fileContent"].ToString(), Encoding.UTF8, file["File"]["contentType"].ToString());
var filePost = await spClient.PostAsync(requestString, filePostData);
}
The contentType of each file varies, while testing for example, I get back these file types:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/pdf
siteResponseObject is the response from creating the listItem which contains it's ID and everything.
Does the sharepoint API expect to like a byte array? Am I encoding the StringContent object wrong?