Does anyone have an example to upload a file using the Content Server 20.4 API. C# would be great too. I just want to upload a document from anywhere(Local to user) into Content Server
This is my code below. As is, I will get an error for an error "An illegal attempt was made to upload a document". Only way I got this far was by putting the doc in the Upload folder on the server, which isn’t great. Code couldn’t find the doc. on the local machine or through a shared folder.
I have also tried to read the doc in as bytes and add that to the params, but I get an error stating the URI is too long, because I tried to add the file as bytes. The CS documents for v2\nodes Post says the request should be encoded form-urlencoded, but I don’t think that would support me converting the file into bytes. I think I would need to use MultipartFormDataContent. But then I don’t know how to write my parameters because I can’t use a List of KeyValuePair.
Hope to hear from someone.
Byte[] bytes = File.ReadAllBytes("C:\\testing.pdf");
String file = Convert.ToBase64String(bytes);
List<KeyValuePair<string, string>> allIputParams = new List<KeyValuePair<string, string>>();
allIputParams.Add(new KeyValuePair<string, string>("type", "144"));
allIputParams.Add(new KeyValuePair<string, string>("parent_id", "39667393"));
allIputParams.Add(new KeyValuePair<string, string>("file_filename", "My New API PDF"));
allIputParams.Add(new KeyValuePair<string, string>("file_content_type", "application/pdf"));
allIputParams.Add(new KeyValuePair<string, string>("file", "testing.pdf"));
//allIputParams.Add(new KeyValuePair<string, string>("file", file));
FormUrlEncodedContent? content_ = new System.Net.Http.FormUrlEncodedContent(allIputParams);
MultipartFormDataContent
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
HttpResponseMessage response = await client.PostAsync("v2/nodes", content_);
string? responseContent = await response.Content.ReadAsStringAsync();