CS 20.4 API Upload document example

Options

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();


Tagged:

Comments

  • In my blog livelink.in I have a Java example . I can tell you how I arrived at that . I found using google how I could attach my eclipse code Java to go through fiddler . Then I ran postman and understood how the multipart form data was constructed. Then with trial and error I wrote the java equivalent. The postman code revelation button shows you stuff but they all depend on external high level libraries I had a need to do this in POJO . What you want to know is the file is given to the content server/ livelink webserver chi using a file input control so if it is a desktop file you have to pass that as a file object that is the only way CS will do the upload . The rest is another multipart entry . No good examples exist at OT mostly there are snippets using JavaScript also @"Ferdinand Prantl" has some examples using powershell and perhaps one C# . I am pretty sure you could find code to make your visual studio proxy through fiddler and try to do what I did. I think you are doing everything correct except you are probably missing something that the postman payload is doing . If my post helps you post a good c# example and I could host it in my page giving you the credits .
  • Jeff Wowk
    edited August 11, 2022 #3
    Options

    Morning, Below is my solution, just a proof of concept. I hope someone finds it helpful. After some googling I figured out how to rewrite my code to use multipartFormContent, put my file into a ByteArrayContent and receive the results from the successful upload. I don’t know if you can use a FileStream, I would be interested to see someone’s solution for that. “Results” is my custom class NewNode. When creating your class, I would suggest examining the response JSON yourself and creating your class from that. The documentation is not accurate.


    private async void DocNew() {
          using (var multipartFormContent = new MultipartFormDataContent()) 
          {
            string filepath = "C:\\testing.pdf";
            //Add other fields
            multipartFormContent.Add(new StringContent("144"), name: "type");
            multipartFormContent.Add(new StringContent("39667393"), name: "parent_id");
            multipartFormContent.Add(new StringContent("My New PDF.pdf"), name: "name");
            //multipartFormContent.Add(new StringContent("My New API PDF"), name: "file_filename");
            //multipartFormContent.Add(new StringContent("application/pdf"), name: "file_content_type");
    
            //Add file
            Byte[] bytes = File.ReadAllBytes("C:\\testing.pdf");
            String file = Convert.ToBase64String(bytes);
            var byteContent = new ByteArrayContent(bytes);
    
            //var fileStreamContent = new StreamContent(File.OpenRead(filepath));
            //fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            multipartFormContent.Add(byteContent, name: "file", fileName: "testing.pdf");
    
            //Send it
            HttpResponseMessage response = await client.PostAsync("v2/nodes", multipartFormContent);
            string? responseContent = await response.Content.ReadAsStringAsync();
    
            NewNode Results = Newtonsoft.Json.JsonConvert.DeserializeObject<NewNode>(responseContent);
          }
        }